Setup vscode for ROS code completion and debugging

ROS extension: code completion and debugging in vscode

May 5, 2023
This article describes setup vscode for ROS code completion and debugging. We will use ROS extension of vscode to accomplish the two.

Code completion

In general, vscode can perform code completion for C++ code in one three ways:

  1. CMake extension
  2. compile_commands.json (opens in a new tab) from c/c++ extension
  3. c_cpp_properties.json from c/c++ extension w/ ROS extension

1. CMake extension (skip if busy)

For a pure cmake project, definitely go to the the first method as I described in this post. But this becomes very tough when you are in ROS2 project as it involved ament_cmake. I still could not find a way to a clear way to do this using CMake extension.

2. compile_commands.json

The second method is well suited to ROS2 project, as colcon build can generate compile_commands.json (opens in a new tab). Actually, this method is officially chosen method for CLion (opens in a new tab).

  • Build with colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON.

  • This will create ~/ros2_ws/build/compile_commands.json.

  • Use this file inside c_cpp_properties.json as below:

      "configurations": [
          "browse": {
            "databaseFilename": "${default}",
            "limitSymbolsToIncludedHeaders": false
          "includePath": [],
          "compileCommands": "/home/jbs/ros2_ws/build/compile_commands.json",
          "name": "ROS",
          "intelliSenseMode": "gcc-x64",
          "compilerPath": "/usr/bin/gcc",
          "cStandard": "gnu11",
          "cppStandard": "c++17"
      "version": 4
  • See? Without any c_cpp_properties.json file, code completion will work.

    compile_commands.json code completion

3. c_cpp_properties.json

Debugging for running node (gdb attaching)

  • Run sudo sysctl -w kernel.yama.ptrace_scope=0 to have authority for gdb attaching to a running process.
  • Follow the below video to add debug setting for ros type.
  • Attach debugging and hit breakpoint.