Robotics StackExchange | Archived questions

Error in vscode: "ros/ros.h"- No such file or directory

Hi all, i am new in ROS and am trying to setup my vscode environment for ROS. I searched a lot for the right answer but could not solve my problem. I inlcuded all important lines in my CMakeList but the error is still not gone, so i am clearly missing something. I am using melodic 1.14.10 on Ubuntu.

Here is my CMakeList:

cmake_minimum_required(VERSION 3.0.2)
project(hello)


find_package(catkin REQUIRED COMPONENTS
  nav_msgs
  roscpp
  rospy
  sensor_msgs
  std_msgs
  geometry_msgs
  message_filters 
)


catkin_package(
  INCLUDE_DIRS include
  LIBRARIES hello
  CATKIN_DEPENDS nav_msgs roscpp rospy sensor_msgs std_msgs geometry_msgs message_filters
  DEPENDS system_lib
)


include_directories(
  #include/hello
  ${catkin_INCLUDE_DIRS}
)

add_library(${PROJECT_NAME}
   src/ekf.cpp
)

## add_executable(EKF src/main.cpp)
add_executable(EKF src/main.cpp src/ekf.cpp)

target_link_libraries(EKF ${catkin_LIBRARIES})

My tasks.json:

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: cpp Aktive Datei kompilieren",
            "command": "/usr/bin/cpp",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Vom Debugger generierte Aufgabe."
        }
    ],
    "version": "2.0.0"
}

My simple header file with the #include ros/ros.h causing the problem:

#include <ros/ros.h>
#include <std_msgs/String.h>
#include <sensor_msgs/LaserScan.h>
#include <nav_msgs/Odometry.h>
#include "tf/tf.h"
#include <geometry_msgs/Twist.h>

#include <fstream>
#include <cmath>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

class EKF{
    public:

        vector<double> landmarks;
        int count;

        vector<double> readFile(ros::NodeHandle nh);

};

I could not attach a screenshot to show the error, but it says exactly the this line:

"ros/ros.h"- No such file or directory

I really appreciate your support.

Asked by helloros on 2022-06-11 13:31:40 UTC

Comments

Answers

In your .vscode/c_cpp_properties.json

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

in your .vscode/settings.json for autocomplete python

{
"editor.tabSize": 4,
"ros.distro": "noetic",
"ros.rosSetupScript": "/opt/ros/noetic/setup.bash",

"python.autoComplete.extraPaths": [
    "/opt/ros/noetic/lib/python3/dist-packages"
],
}

Asked by aarsh_t on 2022-06-11 16:08:44 UTC

Comments

Thank you for your response!

I have created my workspace from scratch and added your .vscode/c_cpp_properties.json and .vscode/settings.json but nothing has changed on the ros/ros.h error.

Now it looks like this:

{
  "configurations": [
    {
      "browse": {
        "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
        "limitSymbolsToIncludedHeaders": false
      },
      "includePath": [
        "/opt/ros/melodic/include/**",
        "/home/kubra/catkin_ws/src/mus1/include/**",
        "/usr/include/**"
      ],
      "name": "ROS",
      "intelliSenseMode": "gcc-x64",
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "gnu11",
      "cppStandard": "c++14"
    }
  ],
  "version": 4
}

And my settings.json:

{
    "editor.tabSize": 4,
    "ros.distro": "melodic",
    "ros.rosSetupScript": "/opt/ros/noetic/setup.bash",

    "python.autoComplete.extraPaths": [
        "/opt/ros/melodic/lib/python2.7/dist-packages"
    ],
    }

But still he error is showing up. What else can i do to make the error dissappear?

Asked by helloros on 2022-06-12 13:45:52 UTC

Comments

"ros.rosSetupScript": "/opt/ros/noetic/setup.bash",

Since you're using melodic, change noetic to melodic.

Also, vscode gives you one of the lightbulbs if it detects an error. If you click that, does it give you a solution? Sometimes if your include path is close enough but not perfect, it realizes what you're trying to do and gives you the option to have vscode fix it.

Asked by Joe28965 on 2022-06-13 05:18:59 UTC

For the code snippet in the task.json:

"args": [
             "-g",
             "${file}",
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}"
          ],

g++ doesn't build your source file with -I option, which means the ROS header is not included.

You can create a new task with

    {
        "type": "shell",
        "command": "catkin build",
        "problemMatcher": [],
        "label": "catkin build",
    },

This will save a lot of trouble with constructing g++ options and build directly from CMakeList.txt.

Asked by JohnDoe on 2023-07-31 20:10:50 UTC

Comments