打开一个C++项目,安装以下两个插件
(一)安装插件
(二)配置文件
按F5,选择g++ build
选择Debug Anyway
选择Open ‘launch.json’
在.vscode文件夹下生成launch.json和tasks.json两个文件
首先编辑task.json,问了对照起见,程序中将要修改的代码注释掉而不是删掉
修改"label"和"command",label可以修改成任意,但是需要和launch.json中的"preLaunchTask"相同,“type"修改成"shell”,然后删除"args"中的所有内容,如下所示:
{
"tasks": [
{
//"type": "cppbuild",
"type": "shell",
// "label": "C/C++: g++ build active file",
// "command": "/usr/bin/g++",
"label": "build",
"command": "cd ${workspaceFolder}/build;cmake ..;make -j12",
"args": [
//"-fdiagnostics-color=always",
//"-g",
//"${file}",
//"-o",
//"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
修改launch.json
默认的launch.json如下,
可以在菜单中选择
此时出现如下窗口,选择c++ launch即可
然后修改"program"标签,修改成需要执行的可执行文件,然后添加添加标签"prelaunchTask":“build”,记得前面]后面加,
修改后如下:
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
//"program": "enter program name, for example ${workspaceFolder}/a.out",
"program": "${workspaceFolder}/build/utils_markermap/aruco_simple_markermap",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}