VS Code IntelliSense Include Path Fix

Tip

When working with ROS2 C++ nodes in VS Code, you might see squiggles / errors on #include "rclcpp/rclcpp.hpp", even though the code compiles with colcon build. This happens because VS Code’s C++ extension cannot find the ROS2 headers by default.


Why it happens

  • VS Code relies on IntelliSense include paths to locate header files.
  • ROS2 headers (rclcpp, std_msgs, etc.) are not in standard system include directories.
  • Without telling VS Code where to look, the editor shows #include errors, even though g++ or colcon can compile the code.

How to fix it

  1. Source ROS2 and your workspace before opening VS Code:
source /opt/ros/jazzy/setup.bash
source ~/ros2_ws/install/setup.bash
  1. Create / update c_cpp_properties.json in .vscode/:
{
  "configurations": [
    {
      "name": "Linux",
      "includePath": [
        "${workspaceFolder}/src/**",
        "${workspaceFolder}/install/**",
        "/opt/ros/jazzy/include/**"
      ],
      "defines": [],
      "compilerPath": "/usr/bin/g++",
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "linux-gcc-x64"
    }
  ],
  "version": 4
}
  • ${workspaceFolder}/src/** → local package headers
  • ${workspaceFolder}/install/** → workspace-installed ROS2 packages
  • /opt/ros/jazzy/include/** → ROS2 system headers
  1. Reload VS Code window (Ctrl+Shift+P → Reload Window) to refresh IntelliSense.

Optional: Use compile_commands.json for perfect IntelliSense

  • Generate it with colcon:
cd ~/ros2_ws
colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
  • Point VS Code to it:
"compileCommands": "${workspaceFolder}/build/my_package/compile_commands.json"
  • IntelliSense now matches the exact compiler flags, avoiding false errors.

✅ Result: Squiggles for #include "rclcpp/rclcpp.hpp" disappear, and VS Code provides accurate autocomplete, go-to-definition, and linting for ROS2 C++ nodes.