1. MSYS2
國(guó)內(nèi)使用中科大的源踱阿,詳見(jiàn):https://lug.ustc.edu.cn/wiki/mirrors/help/msys2
pacman -S mingw-w64-x86_64-toolchain
1.1 踩過(guò)的坑
初始直接安裝
pacman -S gcc
而gcc -version 后發(fā)現(xiàn)是 7.4 而非 8.2 的寻拂。
后使用指令后版本變成 8.2 了固棚。
pacman -S mingw-w64-x86_64-gcc
但后來(lái) gdb 時(shí)還需要手動(dòng)安裝一下落剪。若空間足夠耸采,可以手動(dòng)安裝艳吠。
pacman -S mingw-w64-x86_64-gdb
2. 使用 make 跑一下
test.cpp
#include<iostream>
int main(void) {
long int tag = __cplusplus;
if(tag == 201703L) std::cout << "C++17\n";
else if(tag == 201402L) std::cout << "C++14\n";
else if(tag == 201103L) std::cout << "C++11\n";
else if(tag == 199711L) std::cout << "C++98\n";
else std::cout << "pre-standard C++\n";
return 0;
}
makefile
CPPFLAGS=-Wall -std=c++17
all:test
clean:
rm test.exe
結(jié)果:
$ ./test.exe
C++17
3. 使用 Visual Studio Code
3.1 安裝擴(kuò)展:C/C++
3.2 新建文件夾
File -> Open Folder福也, Create a new Folder 'test'
create a new file test.cpp局骤, the same as 2.
3.3 配置文件
create a dir .vscode with 3 files.
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"c:\\msys64\\mingw64\\include",
"c:\\msys64\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.2.1\\include",
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
用于編譯的 tasks
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "g++",
"type": "shell",
"command": "g++",
"args": [
"${file}", "-g", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe", "-std=c++17"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
用于運(yùn)行加載的launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "c:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++"
}
]
}
4. 編譯
快捷鍵:ctrl+shift+B
鼠標(biāo)的用法:
先選中 test.cpp
再Terminal -- Run Task -- g++ -- Continue...
Terminal 中會(huì)有編譯成功的消息。
5. 運(yùn)行 F5
會(huì)出現(xiàn)輸出結(jié)果
C++17