赞
踩
以下内容均针对 Linux 操作系统(包括Windows的Linux子系统WSL2)。
本文是对Linux系统中使用VSCode编译调试C++代码的系列文章的总结,前面三篇文章如下:
在VSCode中编译调试C++代码的本质逻辑:
tasks.json
指定如何生成二进制可执行文件
.sh
生成launch.json
负责配置gdb调试器,包括:指定可执行文件名、命令行参数,以及预执行任务(prelaunchTask)具体过程在这篇文章中详细解释: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++" } ] }
如果不在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++ 生成活动文件" } ] }
具体过程在这篇文章中详细解释: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" } ] }
脚本文件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
的内容基本没有变:
{ "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++" } ] }
{ "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" } ] }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。