Linux环境使用VSCode调试简单C++代码_linux vscode编译c++代码_果壳中的robot的博客-CSDN博客
Linux环境下使用VScode调试CMake工程 - 知乎
1 vscode实现cmake+make指令
我们都知道,对于cmake构建的工程,编译需要以下步骤:
cd build
cmake ..
make
那如何让vscode来帮我们执行呢?答案就是构建下面的task.json文件。构建步骤为:
* 在VSCode的主菜单中,选择 Terminal>Configure Default Build Task,
* 选择 "CMake: build"
* 将生成一个
tasks.json
文件,将其中的内容作相应的替换:
可以看出,上面的
tasks.json
文件主要包含三个命令:
- label为
cmake
的任务:执行shell类型的cmake命令,其参数为../
,执行时所在的目录为${fileDirname}/build。这个命令等价于在build
目录下执行cmake ../
- label为
make
的任务:执行shell类型的make命令,没有参数,执行时所在的目录为${fileDirname}/build。这个命令等价于在build
目录下执行make
- label为
build
的任务:该任务由cmake和make任务组成,也就是将上面两条命令执行的过程组合成一个build任务。
// tasks.json
{"version": "2.0.0","tasks": [{"label": "cmake","type": "shell","command": "cmake","args": [".."],"options": {"cwd": "${workspaceFolder}/yolov8/build"}, },{"label": "make","type": "shell","command": "make","args": [],"options": {"cwd": "${workspaceFolder}/yolov8/build"}, },{"label": "build",// 依赖于上述的两个 label为 cmake和 make的任务"dependsOn":["cmake", "make"]},],
}
2 vscode实现debug run
我们都知道,对于cmake构建的工程,编译需要以下步骤:
1. 在VSCode的上方菜单中,选择 Run -> Add Configuration,会生成一个空白的
launch.json
文件2. 我们要做的就是在该文件中告诉VSCode:用gdb调试前面生成的可执行文件,在
launch.json
文件中添加如下内容:
launch.json
//json.sh
{// 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": "${workspaceFolder}/yolov8/build/app_yolov8","args": [ ],"stopAtEntry": true,"cwd": "${workspaceFolder}/yolov8/build","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","miDebuggerPath": "/usr/bin/gdb"}]
}
其中,
"program"
:用于指定要调试的可执行文件,这里用变量名指代,其值就是helloCMake
"args"
:执行代码时,需要添加的命令行参数prelaunchTask
:在执行gdb调试前,预先需要执行的任务,这里设置为"build"
,就是指定上一节中配置完成的build任务,即在gdb调试前,先执行cmake和make
打上断点,然后按F5,即可实现代码调试