用习惯了Clion集成开发环境,调试程序时,只需要点击一下工具栏上的调试按钮,便可以直接调试程序。不过Clion对ROS的支持不友好,编写ROS代码和调试都使用VS Code。
VS Code在调试程序之前,需要先配置可执行文件的路径。我这里配置使用gdb进行调试。
打开VS Code,点击左侧功能栏中的小虫子,DEBUG AND RUN配置为gdb launch,会弹出配置模板,类似于
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/devel/lib/path_plan_v3/OutdoorPlanner",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
其中,需要把configurations->program填写为你需要调试的可执行程序即可.
另外,如果想使用调试功能,那么在Cmakelists.txt中CMAKE_CXX_FLAGS标志必须添加‘-g’项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -funroll-loops -fpic -Wall -Wno-sign-compare -Werror=return-type -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fpic")
好了,尽情地调试起来吧,别在代码中加一堆printf/cout查找错误了!