针对ROS配置VScode开发环境(catkin_make 或 catkin build)

使用catkin_make

ubuntu 18的vscode配置ros开发环境:

catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes

ctrl+shift+p,配置c_cpp_properties.json,修改成如下:

// "intelliSenseMode": "gcc-x64",
// "compilerPath": "/usr/bin/gcc",
// "cStandard": "gnu11",
// "cppStandard": "gnu++14",
// "compileCommands": "${workspaceFolder}/build/compile_commands.json"
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"

}

注意不仅仅是加上compileCommands,还有defines cStandard等等都要换

如我自己的配置文件:

{
    "configurations": [
        {
            "browse": {
                "databaseFilename": "",
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "/home/kyle/ros/kyle_ws/devel/include/**",
                "/opt/ros/melodic/include/**",
                "${workspaceFolder}/**",             //工作空间内的所有.h文件,之后就不需要在写头文件的绝对或相对路径了
                "/usr/include/**"
            ],
            // "name": "ROS",
            // "intelliSenseMode": "gcc-x64",
            // "compilerPath": "/usr/bin/gcc",
            // "cStandard": "gnu11",
            // "cppStandard": "gnu++14",
            // "compileCommands": "${workspaceFolder}/build/compile_commands.json"
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

使用catkin build

关于CMAKE_EXPORT_COMPILE_COMMANDS

  • 确保catkin_tools 工作空间被设置成生成compile_commands.json文件

  • 可以设置 CMAKE_EXPORT_COMPILE_COMMANDS 的值(ON或者1)去启动

  • 设置完编译参数后需要重构工作空间

  • 之后 compile_commands.json 便被生成

设置编译参数并且重新编译整个工作空间:

catkin config --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
catkin build

如果有其他编译参数也希望被修改,例如 -DCMAKE_BUILD_TYPE=Debug.

另一种选择是直接修改.catkin_tools/profiles/<profile_name>/config.yaml 文件的 cmake_args 部分:

cmake_args:
- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
- -DCMAKE_BUILD_TYPE=Debug

在VS code中可以智能索引:

ctrl+shift+p打开c_cpp_properties.json

将如下部分:

"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",

改成

"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "gcc-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",

更多推荐