以下内容均针对 Linux 操作系统(包括Windows的Linux子系统WSL2)。
本文是对Linux系统中使用VSCode编译调试C++代码的系列文章的总结,前面三篇文章如下:
- 详解C/C++代码的预处理、编译、汇编、链接全过程
- Linux环境使用VSCode调试简单C++代码
- Linux环境使用VSCode调试CMake工程
1. 根本逻辑
在VSCode中编译调试C++代码的本质逻辑:
tasks.json
指定如何生成二进制可执行文件- 可以直接通过g++编译器生成- 可以通过CMake生成- 可以通过脚本.sh
生成launch.json
负责配置gdb调试器,包括:指定可执行文件名、命令行参数,以及预执行任务(prelaunchTask)
2. 方案一:直接调用g++编译器生成可执行文件
具体过程在这篇文章中详细解释:Linux环境使用VSCode调试简单C++代码
这里简要概括重点内容。
- 第一步:将
*.cpp
源代码文件通过g++
编译器生成一个可调试的可执行二进制文件
如果不在VSCode中运行,而是在终端中运行,需要运行下面的指令:
g++ -g hello.cpp -o hello
那么将这一步配置在VSCode的
tasks.json
中,
tasks.json
中的内容应该如下(具体过程可以参考上面的链接):
{"version":"2.0.0","tasks":[{"type":"cppbuild","label":"C/C++: g++ 生成活动文件","command":"/usr/bin/g++","args":["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options":{"cwd":"${fileDirname}"},"problemMatcher":["$gcc"],"group":{"kind":"build","isDefault":true},"detail":"编译器: /usr/bin/g++"}]}
- 第二步:调用gdb调试器对可执行文件进行调试
如果不在VSCode中运行,而是在终端中运行,需要运行下面的指令:
gdb hello
将这一步配置在VSCode的
launch.json
中,则
launch.json
中的内容应该如下(详细过程和解释同样可以参考上面链接):
{"version":"0.2.0","configurations":[{"name":"(gdb) 启动","type":"cppdbg","request":"launch","program":"${fileDirname}/${fileBasenameNoExtension}","args":[],"stopAtEntry":false,"cwd":"${fileDirname}","environment":[],"externalConsole":false,"MIMode":"gdb","setupCommands":[{"description":"为 gdb 启用整齐打印","text":"-enable-pretty-printing","ignoreFailures":true},{"description":"将反汇编风格设置为 Intel","text":"-gdb-set disassembly-flavor intel","ignoreFailures":true}],"miDebuggerPath":"/usr/bin/gdb","preLaunchTask":"C/C++: g++ 生成活动文件"}]}
3. 方案二:CMake生成可执行文件
具体过程在这篇文章中详细解释:Linux环境下使用VScode调试CMake工程
对于CMake工程,如果不使用VSCode,而是使用终端命令行方式进行编译的话,标准做法是:
cd build
cmake ..make
那么将这个过程配置在VSCode的
tasks.json
中,其内容应该如下(详细过程见参考链接):
{"version":"2.0.0","tasks":[{"label":"cmake","type":"shell","command":"cmake","args":["../"],"options":{"cwd":"${fileDirname}/build"},},{"label":"make","type":"shell","command":"make","args":[],"options":{"cwd":"${fileDirname}/build"},},{"label":"build","dependsOn":["cmake","make"]},],}
通过VSCode完成CMake编译过程后,将会在 build 目录下生成一个可执行文件。
调用gdb对生成的可执行文件进行调试,需要配置
launch.json
文件如下:
{"version":"0.2.0","configurations":[{"name":"g++ - Build and debug active file","type":"cppdbg","request":"launch","program":"${fileDirname}/build/${fileBasenameNoExtension}","args":["para1","para2"],"stopAtEntry":false,"cwd":"${fileDirname}","environment":[],"externalConsole":false,"MIMode":"gdb","setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"preLaunchTask":"build","miDebuggerPath":"/usr/bin/gdb"}]}
4. 方案三:shell脚本生成可执行文件
脚本文件
build_executable.sh
的内容如下:
echo"Configuring and building ORB_SLAM3..."
mkdir build
cd build
# cmake .. -DCMAKE_BUILD_TYPE=Debug
cmake ..-Wno-dev
make -j8
该方案与方案二类似,即把cd build + cmake + make的过程写到shell脚本文件里,那么只需要把方案二中的cmake + make过程替换为执行
.sh
脚本文件即可,直接列出来参考的
tasks.json
和
launch.json
文件,可以发现,
launch.json
的内容基本没有变:
tasks.json
{"version":"2.0.0","tasks":[{"type":"cppbuild","label":"C/C++: g++ build active file","command":"sh","args":["build_executable.sh"],"options":{"cwd":"${workspaceFolder}"},"problemMatcher":["$gcc"],"group":{"kind":"build","isDefault":true},"detail":"compiler: /usr/bin/g++"}]}
launch.json
{"version":"0.2.0","configurations":[{"name":"g++ - debug active file","type":"cppdbg","request":"launch","program":"${fileDirname}/${fileBasenameNoExtension}","args":[],"stopAtEntry":false,"cwd":"${fileDirname}","environment":[],"externalConsole":false,"MIMode":"gdb","setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"preLaunchTask":"C/C++: g++ build active file","miDebuggerPath":"/usr/bin/gdb"}]}
版权归原作者 果壳中的robot 所有, 如有侵权,请联系我们删除。