MinGW
- 官网下载 MinGW并安装。
- 在设置->高级系统设置->环境变量中给PATH添加上MinGW下的bin目录的路径,如D:\mingw-w64\mingw64\bin。
打开cmd,输入g++ -v或gcc -v,如果有输出则配置成功。Using built-in specs.
COLLECT_GCC=gcc
......
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)`
VS code
- 下载安装VS Code。
- VS code中安装 Extension:c/c++ (ms-vscode.cpptools), (Ctrl+Shift+X),安装后重启vs code使之生效。
- 下面逐步展示使用vs code debug cpp,或者参考官网进行配置。后面总共会产生一下几个文件:
- helloword.cpp (示例代码)
- tasks.json (编译配置)
- launch.json (debugger 配置)
- c_cpp_properties.json (compiler path and IntelliSense settings)
- 新建一个helloword目录,并在vscode中选择Open Folder打开该目录
- tasks.json编译
选择Terminal > Configure Default Build Task > C/C++: g++ build active file,产生tasks.json,类似下面:{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:/msys64/mingw64/bin/g++.exe"
}
],
"version": "2.0.0"
}
-
args: 将传给g++的命令行参数. -
${file}: 代码文件, 将编译并在(${fileDirname}) 下生成它对应的可执行文件. 更多细节可参考 variables reference. 点击helloword.cpp,在Terminal工具栏中选择Run Build Task,或者Ctrl+Shift+B 编译该项目。点击下面TERMINAL右边的’+'新建一个TERMINAL,输入.\helloword.exe运行程序 > Executing task: C/C++: g++.exe 生成活动文件 <
正在启动生成...
D:\software\mingw-w64\mingw64\bin\g++.exe -fdiagnostics-color=always -g D:\dev\vscode\projects\helloworld\helloword.cpp -o D:\dev\vscode\projects\helloworld\helloword.exe
生成已成功完成。
Terminal will be reused by tasks, press any key to close it.
PS D:\dev\vscode\projects\helloworld> .\helloword.exe
Hello C++ World from VS Code and the C++ extension!
-
launch.json debug 工具栏选择Run->Add Configuration->C++ (GDB/LLDB)->g++.exe build and debug active file. {
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"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": "C/C++: g++.exe build active file"
}
]
}
Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging. The preLaunchTask setting is used to specify task to be executed before launch. Make sure it is consistent with the tasks.json file label setting. 在helloword.cpp中设置断点,然后选择 Run > Start Debugging或者F5就开始debug了。 -
c_cpp_properties.json, intelliSense Ctrl+Shift+P 输入C/C++: Edit Configurations (UI) 设置编译器路径 compilerPath 和对应的intelliSenseMode。生成c_cpp_properties.json intelliSense是vs code里的一个插件,功能包括:自动代码补全;实时错误检查;代码改进建议。 {
"configurations": [
{
"name": "Linux",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
|