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.
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).
/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 vscodec_cpp_properties.json
file'sincludePath
. but it is not still detecting the path and the error still prevails. $\endgroup$