ros2 使用vscode进行调试时的 task.json和launch.json配置

运行–添加配置

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "colcon",
        "args": [
          "build",
          "--symlink-install",
          "--packages-select",
            "parking_planner",  // 明确指定包名,加速编译
          "--event-handlers",
            "console_cohesion+",
          "--cmake-args",
            "-DCMAKE_BUILD_TYPE=Debug",  // 改为 Debug 模式
            "-DPYTHON_EXECUTABLE=/usr/bin/python3"  // 显式指定Python路径  根据实际修改
        ],
        "problemMatcher": ["$gcc"],   // 通用 problemMatcher
        "group": "build",
        "label": "colcon: build parking_planner"  // 任务标签更明确
 
      }
    ]
  }

lanuch.json

{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “ROS2 Debug parking_area_detector”,
“type”: “cppdbg”,
“request”: “launch”,
“program”: “workspaceFolder/install/parkingareadetector/lib/parkingareadetector/parkingareadetector","args":[],"stopAtEntry":false,"cwd":"{workspaceFolder}/install/parking_area_detector/lib/parking_area_detector/parking_area_detector", "args": [], "stopAtEntry": false, "cwd": "workspaceFolder/install/parkingareadetector/lib/parkingareadetector/parkingareadetector","args":[],"stopAtEntry":false,"cwd":"{workspaceFolder}”,
“environment”: [
{
“name”: “LD_LIBRARY_PATH”,
“value”: “${env:LD_LIBRARY_PATH}:/opt/ros/humble/lib” // 补充 ROS2 库路径
}
],
“externalConsole”: false,
“MIMode”: “gdb”,
“miDebuggerPath”: “/usr/bin/gdb”,
“setupCommands”: [
{
“description”: “Enable pretty-printing”,
“text”: “-enable-pretty-printing”,
“ignoreFailures”: true
},
{
“text”: “set follow-fork-mode child” // 合并到单个 setupCommands
}
],
“preLaunchTask”: “colcon: build parking_area_detector”, // 与 tasks.json 的 label 一致
“logging”: {
“moduleLoad”: false,
“trace”: true
}
}
]
}`

### 配置 ROS2 C++ 项目在 VSCode 的单步调试 #### 创建并配置 launch.json 文件 为了实现单步调试,需创建 `.vscode` 文件夹下的 `launch.json` 文件。此文件用于定义启动配置,允许开发者指定调试器的行为。对于 ROS2 节点而言,一个典型的配置如下所示: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/install/<package_name>/lib/<package_name>/<node_executable>", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: cmake build active file", "miDebuggerPath": "/usr/bin/gdb" } ] } ``` 上述 JSON 片段展示了如何通过修改 `"program"` 字段来指向具体的可执行文件位置[^1]。 #### 修改 CMakeLists.txt 支持调试信息 确保 CMakeLists.txt 中包含了 `-DCMAKE_BUILD_TYPE=Debug` 参数,以便编译过程中加入必要的调试符号。这有助于提高调试效率准确性[^3]。 #### 编辑 c_cpp_properties.json 文件 当遇到头文件路径无法解析的情况,可以通过编辑 `c_cpp_properties.json` 来解决这个问题。通常情况下,VSCode 提供了一个便捷的方式——即点击带有波浪线的错误提示旁的小灯泡图标,并选择相应的修复建议。这样可以自动生成或更新现有的 `c_cpp_properties.json` 文件,从而正确识别包含路径其他编译选项[^2]。 #### 添加 compile_commands.json 到工作区 为了让 IntelliSense 更加智能地理解代码结构,推荐向 `.vscode/settings.json` 或者直接在根目录下添加 `"compileCommands": "${workspaceFolder}/build/compile_commands.json"` 这样的键值对。这样做可以让工具更好地支持代码补全等功能[^5]。 完成以上步骤之后,就可以按下 F5 键开始调试会话了。此应该能够顺利进入断点并逐步跟踪程序逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值