You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下VS Code调试ROS项目:可运行但断点无法触发

ROS Noetic下LSD-SLAM在VS Code中无法触发断点的解决办法

问题根因

用roslaunch启动节点时,gdb无法自动定位到catkin_ws/devel/lib/下可执行文件的调试符号,且调试会话未继承完整的ROS环境变量,导致断点地址解析失败。而直接启动的ROS节点能正常调试,是因为进程启动时的环境与调试上下文完全一致。

具体修复步骤

1. 强制编译生成调试符号

先确保编译时关闭优化,生成完整调试信息——编译器默认的Release模式会剔除调试符号:

# 使用catkin_make编译
catkin_make -DCMAKE_BUILD_TYPE=Debug

# 使用catkin build编译
catkin build --cmake-args -DCMAKE_BUILD_TYPE=Debug

2. 修改launch文件,让节点通过gdb启动

找到lsd_slam的launch文件,给需要调试的节点添加launch-prefix="xterm -e gdb --args",示例如下:

<node name="lsd_slam_core" pkg="lsd_slam_core" type="lsd_slam_core" output="screen"
      launch-prefix="xterm -e gdb --args">
    <!-- 原节点参数无需修改 -->
</node>

启动roslaunch时会自动弹出gdb终端窗口,先不要输入run,等待后续在VS Code中附加进程。

3. 配置VS Code调试会话附加到目标进程

  • 按Ctrl+Shift+D打开调试面板,点击创建launch.json文件并选择ROS环境。
  • 将默认配置替换为attach类型的调试配置,指定可执行文件路径和进程选择逻辑:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to LSD-SLAM Core",
            "type": "cppdbg",
            "request": "attach",
            "program": "${workspaceFolder}/devel/lib/lsd_slam_core/lsd_slam_core",
            "processId": "${command:pickProcess}",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • 启动roslaunch后,在VS Code调试面板选中该配置并点击启动,随后在弹出的进程列表中选择lsd_slam_core进程。

4. 确保VS Code加载完整ROS环境

  • 必须从已source过ROS环境变量的终端打开VS Code,避免环境变量缺失:
source ~/catkin_ws/devel/setup.bash
code .
  • 若嫌手动source麻烦,可在VS Code的settings.json中配置自动加载:
{
    "ros.distro": "noetic",
    "ros.setupScript": "/home/你的用户名/catkin_ws/devel/setup.bash"
}

5. 验证调试符号是否存在

若上述步骤无效,先检查可执行文件是否包含调试符号:

file ~/catkin_ws/devel/lib/lsd_slam_core/lsd_slam_core

输出中包含with debug_info则说明调试符号正常生成;若无此内容,重新执行步骤1的编译命令。


内容的提问来源于stack exchange,提问作者Steve White

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 12:43:29