vscode gdb断点调试,以Vins-Fusion为例

前提:
Vins-Fusion已经可以编译通过、运行正常。

有过嵌入式开发经验都想在调试开源slam系统的时候,可以进行断点调试、单步调试等。如果可以,那对于学习复杂的slam系统来说可以说是如虎添翼。本文就以Vins-Fusion为例,介绍C/C++插件集成的gdb调试功能,及如何利用vscode进行断点调试。
vscode 调试ROS程序的基本思路:1.插件安装;2…/vscode配置。

1.插件安装

(1)vscode左边栏的EXTENSIONS中,搜索“C/C++”,点击并安装;

(2)搜索“ROS”,点击并安装;

2…/vscode配置

在./vscode文件夹下,分别创建c_cpp_properties.json,task.json,launch.json

(1)快捷键ctrl+shift+p,选择c/c++:Edit configurations(JSON),如下图所示

vscode gdb断点调试,以Vins-Fusion为例_第1张图片
在./vscode会生成c_cpp_properties.json文件,文件配置如下:

c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/usr/include/eigen3",
                "/opt/ros/noetic/include",
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
            "mergeConfigurations": false,
            "browse": {
                "path": [
                    "${workspaceFolder}/**"
                ],
                "limitSymbolsToIncludedHeaders": true
            }
        }
    ],
    "version": 4
}

其中:compile_commands.json,是编译vins_fusion生成的,编译命令为:
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1

(2)Ctrl+shift+P 进入命令模式,键入tasks: Configure Task,选择: catkin_make:build,可以点击配置设置为默认,修改.vscode/tasks.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",
            "command": "catkin_make",
            "args": [],
            "group": "build",
            "presentation": {
                "reveal": "always"
            },
            "problemMatcher": "$msCompile"
        },
    ]
}

没问题后,快捷键 ctrl + shift + B 可以成功编译了。

(3)GDB debug的配置

GDB调试器是调试C++代码的神器,vins_fusion本质上也是一个ROS项目,因此也可以用GDB进行调试
在vscode里面已经继承了GDB调试器,我们需要做的就是配置launch.json文件,点击左侧工具栏”Debug“,点击”齿轮“按钮,或者快捷键ctrl + shift + D,此时.vscode文件夹下面就会自动生成launch.json文件,配置如下所示:

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": "vins_node",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/vins/vins_node",
            "args": ["${workspaceFolder}/src/VINS-Fusion/config/realsense_d435i/realsense_stereo_imu_config.yaml"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为gdb启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true,
                }
            ],
        }
    ],
}

configurations关键参数依次為节点名,类型,运行请求方式,程序入口,参数。若需添加多个节点只需按照上述格式再添加即可。

3.启动调试:

1.运行roscore

2.ctrl + shift + D在下图红色标记框内选择需调试的节点,点击开始即可。

vscode gdb断点调试,以Vins-Fusion为例_第2张图片

从图中可以看出vins_fusion已经启动,等待image和imu数据。这时你就可以随处断点,开心调试啦。

注:可以提前录好的数据包或者找数据集进行系统调试。

参考:
https://blog.csdn.net/weixin_35695879/article/details/85254422

你可能感兴趣的:(slam,vscode,ide,计算机视觉,自动驾驶,机器人,1024程序员节)