1、生成compile_commands.json文件,解决部分头文件找不到问题
执行编译命令
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes
即可在ROS工作空间的build文件夹下面生成compile_commands.json,然后在c_cpp_properties.json文件添加
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
即可找到全部头文件,下面为添加后的c_cpp_properties.json文件
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/home/mdj/catkin_ws/tiejian_lidar_perceive/devel/include/**",
"/opt/ros/melodic/include/**",
"/home/mdj/catkin_ws/tiejian_lidar_perceive/src/ivcommon/include/**",
"/home/mdj/catkin_ws/tiejian_lidar_perceive/src/lidar_object_detector/include/**",
"/home/mdj/catkin_ws/tiejian_lidar_perceive/src/points_preprocess/include/**",
"/usr/include/**",
"/home/mdj/catkin_ws/tiejian_lidar_perceive/src/sensor_driver/src/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}
至此,可以实现代码的自动跳转了。
2、生成并修改task.json文件,作用是在每次debug之前预编译部分程序
按Ctrl+shift+P进入命令模式,键入tasks: Configure Task,选择 cmake:build 选项生成task.json文件。
修改完成后的task.json文件如下所示
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "catkin_make", //代表提示的描述性信息
"type": "shell", //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
"command": "catkin_make",//这个是我们需要运行的命令
"args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
"group": {"kind":"build","isDefault":true},
"presentation": {
"reveal": "always"//可选always或者silence,代表是否输出信息
},
"problemMatcher": "$msCompile"
}
]
}
其中,这行设置 “group”: {“kind”:“build”,“isDefault”:true},代表将我们定义的这个task添加到build组里面,这样就可以中Ctrl+Shift+B快捷键来找到编译命令,命令名称就是在label里面定义的,如果"isDefault":true那么就代表直接执行command,如果为false还需要在build下拉里面选一下,我们这里就是label名字:catkin_make
3、生成并修改launch.json文件,设置launch启动文件绝对路径
修改后的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": "ROS: Launch", // 配置名称,将会在调试配置下拉列表中显示
"target": "/home/mdj/catkin_ws/tiejian_lidar_perceive/tiejian.launch",
"type": "ros", // 调试器类型 该值自动生成
"request": "launch" // 调试方式,还可以选择attach
}
]
}
4、待调试节点中CMakeLists.txt文件设置
待调试的节点程序还需要在CMakeLists.txt中添加
SET(CMAKE_BUILD_TYPE Debug)
之后继续执行
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes
5、打断点debug操作步骤
先打开一个roscore,然后在节点程序中需要的位置打断点,最后点击如图所示左侧ROS::Launch即可开始debug
参考
https://blog.csdn.net/ABC_ORANGE/article/details/102665792
https://blog.csdn.net/Kalenee/article/details/103828448
【ROS】 在VScode中 ROS Debug 配置方法非常详细版