Linux系统中使用vscode调试C++代码

1.内容说明

最近想重新体验一下debug C++程序,之前都没有尝试过用VSCODE调试C++程序。最近搜集资料并进行了尝试,记录如下
参考链接:
https://github.com/ms-iot/vscode-ros
https://blog.csdn.net/qq_59084325/article/details/125662393
在VScode中ROS Debug配置

2. 前提准备

安装必要插件

  • ROS安装:
    查看ROS官网或者使用鱼香ROS一键安装
  • Vscode插件安装
    • C/C++拓展
    • CMAKE TOOL
    • ROS插件

编译Debug版本

在目录的终端执行

cd ~/catkin_ws
catkin_make -DCMAKE_BUILD_TYPE=Debug
source devel/setup.bash

3.关键文件配置

task.json 编译配置文件(可选)

task.json为编译配置文件,一般打开后会自动在文件目录的.vscode下自动生成。需要自动或者手动填写内容。填写的内容一般如下。无需更改

{
 "version": "2.0.0",
 "tasks": [
   {
     "label": "catkin build (Debug)",
     "type": "shell",
     "command": "catkin_make -DCMAKE_BUILD_TYPE=Debug",
     "group": {
       "kind": "build",
       "isDefault": true
     },
     "problemMatcher": []
   }
 ]
}

launch.json 调试配置文件

launch.json是与调试相关的配置文件,需要手动添加并输入以下内容。通常情况下需要对programcwd参数进行修改,改成对应的文件路径。

ps:点击左侧第四个选项``运行和调试,点击右下角添加配置会自动以gdb启动。

{
 "version": "0.2.0",
 "configurations": [
   {
     "name": "Launch xxx node",
     "type": "cppdbg",
     "request": "launch",
     "program": "${workspaceFolder}/devel/lib/xxx/xxx", #改为可执行文件的绝对路径
     "args": [],
     "stopAtEntry": false,
     "cwd": "${workspaceFolder}", #改为项目工作空间
     "environment": [],
     "externalConsole": false,
     "MIMode": "gdb",
     "miDebuggerPath": "/usr/bin/gdb",
     "setupCommands": [
       {
         "description": "Enable pretty-printing",
         "text": "-enable-pretty-printing",
         "ignoreFailures": true
       }
     ],
       "preLaunchTask": "catkin build (Debug)" #与task.json的name保持一致
   }
 ]
}

c_cpp_properties.json include文件配置(?非必要)

配置 include 路径,帮助 IntelliSense 正常工作:

{
 "configurations": [
   {
     "name": "Linux",
     "includePath": [
       "${workspaceFolder}/**",
       "/opt/ros/noetic/include/**",
       "/usr/include/**",
       "/usr/include/eigen3/**"
     ],
     "defines": [],
     "compilerPath": "/usr/bin/g++",
     "cStandard": "c11",
     "cppStandard": "c++14",
     "intelliSenseMode": "linux-gcc-x64"
   }
 ],
 "version": 4
}

4.手动输入调试

在配置好关键文件之后,就可以通过VSCODE侧边栏的运行和调试(ctrl+shift+D)进行调试。下面以调试LIO-SAM为例进行说明

  1. 安装插件
    在vscode的插件广场中,安装所需的插件

  2. 配置关键文件
    配置tasks.json
    img
    配置launch.json
    img

  3. 开始调试
    img

5.Vscode调试ROS

ROS官方提供了方法能够快速调试ROS的launch文件,给的gif操作如下:

img

  1. 打开右侧debug也就是上一步里面的步骤了,然后点击create new
  2. 在②步红点中 如图输入ROS
  3. 在③步红点中 如图选择ROS: Launch
  4. 然后出现的就是你能source到的所有的launch文件,输入你想launch的文件

img

posted @ 2025-04-09 20:17  遥感摆烂人  阅读(285)  评论(0)    收藏  举报