Ubuntu18.04下VScode调试教程
一、调试准备
首先是下载vscode以及编译器clang-6.0,具体的操作见我的另外两篇博客
关于vscode与clang的配置
ubuntu下的Vscode部署以及代码上传到码云
二、需要创建哪些相关配置文件?
1.创建task.json文件
首先,我们需要一个task.json文件来告诉Visual Studio Code如何构建C++程序,创建的方法有:
(1)复制粘贴现成的task.json或者整个.vscode文件夹(注意都是隐藏文件夹)
(2)点击Terminal–>Configure Default Build Task
注意此操作,要先选中工作空间中的cpp文件,才会跳出第二张图片的内容
下图列出了预定义的构建任务,选择第二个
还有一步是可选操作,如果用了ros2的colcon build命令就不需要去单独构建C++文件了,但如果是一个单纯的C++工程,就需要:Terminal–>Configure Default Build Task(快捷键ctrl+shift+B)去构建可执行文件。这里不详细展开。
2.创建launch.json文件
直接按F5或者Run–>Add Configuration
再选择C++(GDB/LLDB),选择第一个clang-6.0就好了
launch.json能够配置多个可执行文件,实现同时调试,如果想要在现成的文件里添加新的配置,直接点击界面左下方的蓝色键Add Configuration即可,或者复制粘贴
3.创建c_cpp_properties.json文件
相比于其他插件都是把设置项储存在Visual Studio Code的setting.json文件中,C/C++插件是通过c_cpp_properties.json文件来配置编译器、头文件路径、C++标准等设置项的。
创建方式:
通过Ctrl+Shift+P快捷键打开命令面板,然后输入并执行C/C++:Edit Configuration(JSON)就能创建。
4.创建setting.json文件
setting.json文件用来控制诸多工作项的配置,比如代码自动补全等。
创建方式:
通过Ctrl+Shift+P快捷键打开命令面板,然后输入并执行Preferences: Open Settings(JSON)就能创建。
三、怎么设置这些相关配置文件?
关于这一部分,阅读代码的相关的注释即可
1.设置task.json文件
{
"version": "2.0.0",
"tasks": [
// 各任务之间用大括号分割开,大括号之间有逗号隔开。
{
// 这个任务用colcon工具来编译整个工作目录下的packages
// 在修改完代码后启动调试前通常需要先执行此任务。
"type": "shell",
"command": "colcon build ", // 此任务要执行的命令(等价于在终端直接输入 colcon build)
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"label": "colcon build " // 这是任务名称。
},
{
// 此任务用于安装编译完的包
"type": "shell",
"command": "source ${workspaceFolder}/install/setup.sh", // 任务要执行的命令
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": "source", //代表依赖关系,在执行此任务前需要先执行colcon任务。
"problemMatcher": [],
"label": "setup" //此任务的名称,在launch.json文件中需要引用此名称。
},
{
"type": "shell",
"command": "source /opt/ros/dashing/setup.sh", // 任务要执行的命令
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"label": "source" //此任务的名称,在launch.json文件中需要引用此名称。
},
// 还可以继续定义其它想要执行的任务(用大括号隔开)。
// 。。。
]
}
2.设置launch.json文件
{
// 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_pub_map",
"type": "cppdbg",
"request": "launch",
// program代表要调试的程序路径(不同路径程序要对应修改)
"program": "${workspaceFolder}/install/publish_map/lib/publish_map/pub_map",
"args": [],
"stopAtEntry": false,// 是否在启动时,停在入口处。
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
// preLaunchTask代表在启动调试前需要执行的命令(该命令在tasks.json文件中定义)
"preLaunchTask": "setup",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(gdb)sub_map_pub_path",
"type": "cppdbg",
"request": "launch",
// program代表要调试的程序路径(不同路径程序要对应修改)
"program": "${workspaceFolder}/install/publish_map/lib/publish_map/sub_map_pub_path",
"args": [],
"stopAtEntry": false,// 是否在启动时,停在入口处。
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
// preLaunchTask代表在启动调试前需要执行的命令(该命令在tasks.json文件中定义)
"preLaunchTask": "setup",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Python: visualize",
"type": "python",
"request": "launch",
"preLaunchTask": "setup",
// "program": "${workspaceFolder}/install/map_visualize/lib/map_visualize/get_path",
"program": "${workspaceFolder}/src/map_visualize/map_visualize/visualize.py",
"console": "integratedTerminal",
"stopOnEntry": false,
"cwd": "${workspaceFolder}",
}
]
}
3.设置c_cpp_properties.json文件
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
// 添加C++的头文件目录。如果ros安装在不同的位置,对应的修改第二行路径
"includePath": [
"${workspaceFolder}/install/**",
"/opt/ros/dashing/include/**",
"/usr/include/**",
"${workspaceFolder}/src/publish_map/include/**"
],
// 设置C++编译器路径和语言标准,我们采用C++14标准
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/clang-6.0",
"cStandard": "c11",
"cppStandard": "c++14"
}
],
"version": 4
}
4.设置setting.json文件
{
// 在这里添加python3的包地址,这样在源代码里能自动补全,方便写程序。
"python.autoComplete.extraPaths": [
"/opt/ros/dashing/lib/python3.6/site-packages",
"${workspaceFolder}/install/custom_msg/lib/python3.6/site-packages",
"${workspaceFolder}/install/map_visualize/lib/python3.6/site-packages"
],
"python.formatting.provider": "yapf", // 使用yapf自动格式化代码
"python.linting.enabled": false, //禁止lint语法提示(感觉不准)
"files.associations": {
"istream": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"array": "cpp",
"functional": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"vector": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"*.idl": "cpp",
"*.ipp": "cpp",
"*.txx": "cpp",
"unordered_set": "cpp"
},
"ros.distro": "dashing"
}
四、调试方法
在我们配置好后,点击左侧的run,可以看到有三个对应的不同的调试选项分别是:
(gdb) Launch_pub_map
(gdb)sub_map_pub_path
Python: visualize
对应着我们launch.json里面的调试名"name":
比如选中第一个 (gdb)sub_map_pub_path 然后按F5
如果想调试另外一个程序,就点击左侧的run,选中第二个 (gdb)sub_map_pub_path然后此时按F5是无效的
要点击左侧的绿色三角形!
最后我们检查一下一共有三个调试进程,在这里进行调试进程的切换:
再看看我们的终端有几个进程:
最后这里会有一个小问题,我每次debug的第一次会失败,然后在生成的终端里source install/setup.sh然后在次debug就可以了,不知道其他同志会不会有这个问题。
大功告成,开始调试。