1. 程式人生 > 其它 >vscode除錯ros工程

vscode除錯ros工程

vscode除錯ros工程

通過launch檔案啟動,並且加斷點除錯。

1.外掛安裝

  • ROS
  • c++ Intellisense 和c/c++
  • Txt Syntax
  • Msg Language Sopport

2.匯入工作空間

用vscode開啟相應的工程

3.環境配置

需要配置三個檔案

  • task.json配置,用於ctrl+shift+b編譯
{
    // 有關 tasks.json 格式的文件,請參見
        // https://go.microsoft.com/fwlink/?LinkId=733558
        "version": "2.0.0",
        "tasks": [
            {
                "label": "catkin_make:debug", //代表提示的描述性資訊
                "type": "shell",  //可以選擇shell或者process,如果是shell程式碼是在shell裡面執行一個命令,如果是process代表作為一個程序來執行
                "command": "catkin_make",//這個是我們需要執行的命令
                "args": ["-DCMAKE_BUILD_TYPE=RelWithDebInfo","-j8"],//如果需要在命令後面加一些字尾,可以寫在這裡,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
                "group": {"kind":"build","isDefault":true},
                "presentation": {
                    "reveal": "always"//可選always或者silence,代表是否輸出資訊
                },
                "problemMatcher": "$msCompile"
            }
        ]
    }
    
  • c_cpp_properties.json配置
{
    "configurations": [
        {
            "name": "ROS",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/ros/melodic/include/**",
                "/usr/include/**",
                "/home/gao/.../include/**",
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}
  • 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",
            "request": "launch",
            "target": "/home/gao/catkin_hy_ws/../launch/test.launch",
            "type": "ros"
        }
    ]
}

關於程式碼智慧提示:

  • 執行一下命令,在build中生成compile_commands.json
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes
  • 新增c_cpp_properties.json檔案中:
"compileCommands": "${workspaceFolder}/build/compile_commands.json"

4.開始除錯

新增環境變數:

source devel/setup.bash

vscode右側工具欄Run and Debug,ROS:Launch,開始除錯

有時候會報錯,可以重啟ROS外掛試試。