vscode调试C++工程
1. CMakeLists.txt中添加如下内容:
set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
2. 在.vscode中添加launch.json 和 tasks.json两个文件。如下
launch.json文件如下:"program"设置要调试的执行程序路径,${workspaceFolder}是当前文件夹目录
"args":包含了要传递给可执行文件的命令行参数, 程序以./demo arg1 arg2 --option
的形式运行
“miDebuggerPath"是gdb调试器的路径
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/demo",
"args": ["arg1", "arg2", "--option"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/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
}
]
}
]
}
tasks.json如下:
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "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" }
3. 代码设置断点后,点击vscode左侧边栏 (gdb)Launch,绿色run按钮即可进行调试
有时候代码可以正常运行,但调试时 出现一些error,此时是无法正常调试的。