前言
使用vscode配置c++ debug环境的好处
1、可以借助vscode方便轻量的扩展和功能
2、避免了传统使用gdb 复杂按键以及不够直观的可视化
3、方便一次运行,断点处查看变量,降低找bug难度
4、某大公司项目采用类似配置,经过实践检验
配置c++运行环境
MSVC在Windows下编译c/c++
**gcc, g++**分别是GNU的c & c++编译器,在Linux下面用。
cmake的输入是CMakeLists.txt(描述编译过程),输出是makefile。build过程的输入是makefile,输出结果是可执行文件,build的过程会调用编译器和连接器来完成整个过程。
qmake用来build qt工程。
MINGW包含gcc和一系列工具,是windows下的gnu环境,让开发者在windows下可以写gnu的c/c++代码, 编译的结果是windows的可执行文件exe,PE格式的,在windows下运行。
可以自行搜索不同环境的配置方式
配置VSCODE
下载扩展
配置项目
首先我们创建使用Vscode打开项目工程区
然后在该工程下创建如下文件
也就是说当调试一个多文件c++项目时,将使用launch.json进行调试,其依赖于preLaunchTask定义的build任务。
这个任务通过cmake 和make进行构建。参考链接1给出了更加详细的介绍
而如果只是简单调试单文件可以略去task.json的配置,参考链接2给出了更加详细的介绍
launch.json
{"version": "0.2.0","configurations": [{"name": "g++ - Build and debug active file","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/xxxxxx","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "build","miDebuggerPath": "/usr/bin/gdb"}]
}
tasks.json
{"version": "2.0.0","tasks": [{"label": "cmake","type": "shell","command": "cmake","args": ["-B","build"],"options": {"cwd": "${workspaceFolder}"}},{"label": "make","type": "shell","command": "cmake","args": ["--build","build"],"options": {"cwd": "${workspaceFolder}"}},{"label": "build","dependsOn": ["cmake","make"]}],
}
最后一键F5 即可进行断点调试了
参考资料
https://zhuanlan.zhihu.com/p/618043511
https://blog.csdn.net/qq_42417071/article/details/137438374