1--前言:
生成的可执行文件必须为 debug 版本,博主倾向于使用 Cmake 生成可执行文件,因此CMakeLists.txt 必须设置以下参数:
set(CMAKE_BUILD_TYPE Debug)
2--launch.json文件编写:
{
"version": "0.2.0",
"configurations": [
{
"type": "cppdbg", // C++ debug
// 若debug的时候,项目已启动,则设置为attach
// 若debug和启动项目的时机相同,则设置为launch
"request": "launch", // debug的类型,launch表示启动,attach表示附加
"name": "C++ Debug", // 在VSCode侧边栏Run那里显示的名称(人为设定)
//"preLaunchTask": "g++ compile", // 在debug之前要进行的任务即compile,需与tasks.json中编译任务的label相同
"program": "${workspaceFolder}/build/main.exe", // debug的对象(-g编译出来的二进制文件),需与.vscode/tasks.json中生成的可执行文件一致
"args": [], // 运行可执行程序需要添加的输入参数(argc/argv)
"environment": [], // 放置环境变量
"cwd": "${workspaceFolder}", // 当前工作目录
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main中打断点
"externalConsole": true, // 是否使用额外的终端,否则使用内置终端
"MIMode": "gdb", // 指定连接的调试器,gdb或lldb
}
]
}
将上面的 "program" 设置为对应的可执行文件路径即可;
3--测试
在 VSCode 上使用 快捷键F5 即可调试对应的可执行文件;