2
$\begingroup$

Recently, when I was trying to compile a ROS programm written in C++ (with C/C++ extension installed in VSCode) I ran in to a problem with the header files included in the program (rclcpp, etc.) and the error was that those header files couldn't be resolved. When I tried disabling the extension the problem got solved. Is there any other way around to solve this issue?

How to recreate the error

  1. Install the C/C++ extension in VSCode from here.

  2. Make sure that the extension is enabled.

  3. Create a simple ROS package with --build-type as ament_cmake and list some of the dependencies using --dependencies as rclcpp etc. or simply create ROS package with C++ code in it. For example :

    ros2 pkg create SamplePackage --build-type ament_cmake --dependencies rclcpp

  4. Now add some sample code to the src folder and try to include the rclcpp library using #include "rclcpp/rclcpp.hpp" header and you will get the error and the header cannot be resolved.

  5. Below is the screen sample ROS package that I have created with the extension enabled

    Screenshot of the same package with the extension enabled

  6. When the extension is disabled the error goes out. Below is the screenshot of the same package with the extension disabled.

    Screenshot of the same package with the extension disabled

How I tried to solve the issue

I edited the file c_cpp_properties.json (file used to set the C/C++ extension configuration in VSCode) as per given below.

{
  "configurations": [
    {
      "name": "Linux",
      "includePath": ["/opt/ros/humble/include/**/**/", "${workspaceFolder}/**"],
      "defines": [],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c17",
      "cppStandard": "c++14",
      "intelliSenseMode": "linux-clang-x64"
    }
  ],
  "version": 4
}

I tried using multiple variations in the path, and all of it didn't worked for me. I am attaching the screenshot for the folder that contains the header file for ROS 2 down below,

rclcpp_path

The error prevails even after updating the path in the C/C++ extension settings, please refer the screenshot below. enter image description here

$\endgroup$
7
  • $\begingroup$ Hi @Jishnu Please edit your question provide more details. As it's written you've provided that you get a category of error when using a certain tool. This isn't enough to understand what you're doing, what you expect and what you're actually getting. To get help you'll want to provide enough information to fully reproduce your problem to be able to help you solve it. Your high level question of is there another way to do this is almost certainly yes. But without knowing more that's not going to be helpful. $\endgroup$
    – Tully
    Commented Dec 23, 2022 at 17:38
  • $\begingroup$ @Tully , I have edited the question and included method to recreate the same. Hope it can provide more insight about the problem. $\endgroup$
    – Jishnu
    Commented Dec 26, 2022 at 6:11
  • $\begingroup$ How did you install rclcpp? Where is it on your system? How did you setup your environment to be able to find rclcpp? $\endgroup$
    – Tully
    Commented Dec 27, 2022 at 17:33
  • $\begingroup$ I think rclcpp was installed along with the ros distro installation(I use ros2 Humble). Is there a way to check that? $\endgroup$
    – Jishnu
    Commented Dec 28, 2022 at 16:38
  • $\begingroup$ @Tully, I have found out that the rclcpp's include path is /opt/ros/humble/include/ and all the other ros related cpp header packages are found in this location, I even tried adding it to the vscode c_cpp_properties.json file's includePath. but it is not still detecting the path and the error still prevails. $\endgroup$
    – Jishnu
    Commented Jan 18, 2023 at 15:21

3 Answers 3

1
$\begingroup$

I've run your commands from above inside a Docker container that is used within VS Code via the Dev Containers extension. (This is just my favourite method - it's exactly the same as running the commands on a normal Linux bash.)

My devcontainer.json file inside the .devcontainer folder contains following lines:

{
    "name": "ROS 2 C++ extension issue",

    // Sets the run context to one level up instead of the .devcontainer folder.
    "context": "..",

    // docker image to be used for development process
    "image": "ros:humble-ros-base-jammy",

    // Set *default* container specific settings.json values on container create.
    "settings": {},

    // Add the IDs of extensions you want installed when the container is created.
    "extensions": [
        "ms-vscode.cpptools"
    ],

    // Uncomment the next line to run commands after the container is created - for example installing curl.
    "postCreateCommand": "apt-get update && apt-get install -y clang"
}

Once the container is created and started in VS Code I ran source /opt/ros/humble/setup.bash, then ros2 pkg create SamplePackage --build-type ament_cmake --dependencies rclcpp and created a ros_timer.cpp inside the src folder with following content:

// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"

using namespace std::chrono_literals;

/* This example creates a subclass of Node and uses a fancy C++11 lambda
 * function to shorten the timer syntax, at the expense of making the
 * code somewhat more difficult to understand at first glance if you are
 * unaccustomed to C++11 lambda expressions. */

class MinimalTimer : public rclcpp::Node
{
public:
  MinimalTimer()
  : Node("minimal_timer")
  {
    auto timer_callback = [this]() -> void {RCLCPP_INFO(this->get_logger(), "Hello, world!");};
    timer_ = create_wall_timer(500ms, timer_callback);
  }

private:
  rclcpp::TimerBase::SharedPtr timer_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalTimer>());
  rclcpp::shutdown();
  return 0;
}

Now if I open the file I see red squiggles because it cannot find the include file. We've to tell IntelliSense where to search for it, so create a c_cpp_properties.json inside the .vscode folder (example configurations for gcc and clang, note that ROS 2 Humble targets C++17 (not relevant for this example but could lead to problems later on)):

{
    "configurations": [
        {
            "name": "ROS 2 clang",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/ros/humble/include/**"
                "/usr/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}

/*
{
    "configurations": [
        {
            "name": "ROS 2 gcc",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/ros/humble/include/**",
                "/usr/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}
*/

After configuring IntelliSense the red squiggles are gone and I can do a right click on #include "rclcpp/rclcpp.hpp" and go to the definition. I can also right click a function or definition inside the code and e.g. peek its definition. enter image description here

But if I disable the C/C++ extension (and restart VS Code as this seems to be required) the red squiggles are gone as you noticed but I also lost all the C/C++ extension functionality (right click doesn't show much).

enter image description here

$\endgroup$
5
  • $\begingroup$ yeah, thanks for your effort, I have tried what you told and the error is still there, even though vscode complains about the source not found, I am able to go to the header file definition with a right click or F12. I have updated the screenshot of both the config file and error in packages side by side. $\endgroup$
    – Jishnu
    Commented Jan 21, 2023 at 6:26
  • $\begingroup$ What's the exact error message it shows now? And is it the same for all three #include statements? $\endgroup$
    – Bi0T1N
    Commented Jan 21, 2023 at 10:25
  • $\begingroup$ Yes, all the three #include statements gives the same error, and it says cannot open source file . $\endgroup$
    – Jishnu
    Commented Jan 21, 2023 at 15:13
  • $\begingroup$ Which Linux distro do you use? Does it have clang installed? You are also required to use C++17 for Humble as I wrote above (might not be able to parse stuff) but to be honest I can't reproduce your problem... $\endgroup$
    – Bi0T1N
    Commented Jan 31, 2023 at 9:53
  • $\begingroup$ I use ubuntu 22.04 , and it does have clang but I don't think it uses C++17 $\endgroup$
    – Jishnu
    Commented Mar 10, 2023 at 6:20
1
$\begingroup$

You can use the ROS extension in vscode provided by Microsoft, it has the feature of Automatic ROS environment configuration and Automatically add the ROS C++ include and Python import paths. Once extension is added you should use the command pallet (ctrl+shift+p) in vscode to select ROS: Update C++ Path or ROS: Update Python Path. This worked for me.

$\endgroup$
1
  • $\begingroup$ When working without docker and creating new ROS 2 messages in another folder this was the quickest fix to get VS Code to remove the error hint. $\endgroup$ Commented Jun 27 at 11:10
0
$\begingroup$
  1. Update the "includePath": [] configuration in your .vscode/c_cpp_properties.json file with the following:
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/ros/noetic/include/**",
                "/usr/include/eigen3"
            ],
  1. Open your ros workspace with VScode directly code my_ros_ws. both work for me
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.