新建test.cpp文件(以最简单的 HelloWorld.cpp 为例)
#include#include int main() { printf("Hello World\n"); system("pause"); return 0; }
进入调试界面添加配置环境,选择 C++(GDB/LLDB),再选择 g++.exe,之后会自动生成 launch.json 配置文件。
编辑 launch.json 配置文件
{ "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": "${workspaceFolder}", "environment": [], "externalConsole": true, //修改此项,让其弹出终端 "MIMode": "gdb", "miDebuggerPath": "D:\\2Software\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "task g++" //修改此项 } ] }
返回.cpp文件,按F5进行调试,会弹出找不到任务"task g++",选择 "配置任务",会自动生成 tasks.json 文件。
编辑 tasks.json 文件
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "task g++", //修改此项 "command": "D:\\2Software\\mingw64\\bin\\g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:\\2Software\\mingw64\\bin" }, "problemMatcher": [ "$gcc" ], "group": "build" } ] }
【注】: launch.json 文件中 "preLaunchTask" 的值 必须与 tasks.json 文件中 "label"的值一致。值的设置看个人喜好,保持默认也是OK的。
五、运行
返回 HelloWorld.cpp 文件,按F5调试,发现完全OK了!
相关教程分享:vscode教程
以上就是vscode怎么配置c/c++环境的详细内容,更多请关注其它相关文章!