实现基于VS Code的ROS2代码调试配置需要完成以下三个文件的配置:
{
"configurations": [
{
"name": "ROS2",
"includePath": [
"${workspaceFolder}/src/**",
"${workspaceFolder}/install/**/include/**",
"/opt/ros/humble/include/**",
"/usr/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
注:includePath中ROS2版本根据实际情况进行修改。
2、launch.json
{
"version": "0.2.0",
"configurations": [
// 调试服务端节点示例(cpp02_service包)
{
"name": "Debug demo01_server",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/cpp02_service/demo01_server",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "ROS_DOMAIN_ID",
"value": "0"
},
{
"name": "LD_LIBRARY_PATH",
"value": "${workspaceFolder}/install/base_interfaces/lib:${workspaceFolder}/install/cpp02_service/lib:/opt/ros/humble/lib:$LD_LIBRARY_PATH"
},
{
"name": "ROS_PACKAGE_PATH",
"value": "${workspaceFolder}/src:$ROS_PACKAGE_PATH"
}
],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "colcon build", // 调试前自动编译
"miDebuggerPath": "/usr/bin/gdb",
"logging": {
"moduleLoad": false,
"trace": true
}
},
// 调试客户端节点示例(cpp02_service包)
{
"name": "Debug demo02_client",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/cpp02_service/demo02_client",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "ROS_DOMAIN_ID",
"value": "0"
},
{
"name": "LD_LIBRARY_PATH",
"value": "${workspaceFolder}/install/base_interfaces/lib:${workspaceFolder}/install/cpp02_service/lib:/opt/ros/humble/lib:$LD_LIBRARY_PATH"
}
],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "colcon build",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
注:环境变量LD_LIBRARY_PATH中必须添加ROS2的lib路径,负责提示链接库失败问题。
3、tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "colcon build",
"type": "shell",
"command": "colcon build --packages-select base_interfaces cpp02_service cpp02_cs cpp04_action cpp04_action_cs --cmake-args -DCMAKE_BUILD_TYPE=Debug",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "使用colcon编译指定ROS2包"
}
]
}
注:command中执行colcon build时必须添加--cmake-args -DCMAKE_BUILD_TYPE=Debug参数,负责调试时无法进入断点。
参考:VS Code 使用教程 | 人人都懂物联网