[Unit 3] Exercise 3.1 topic_publisher_pkg: fatal error: ros/ros.h: No such file or directory

Hello all,
I am actually trying to complete the Exercise 3.1 of Unit 3. I created the package topic_publisher_pkg according to the instructions given in the previous Unit. However, when I try to build using
$ catkin_make, it raises the error:
fatal error: ros/ros.h: No such file or directory

My CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.0.2)
project(topic_publisher_pkg)

find_package(catkin REQUIRED COMPONENTS
    roscpp
    std_msgs
)

catkin_package(
    # INCLUDE_DIRS include
    # LIBRARIES topic_publisher_pkg
    # CATKIN_DEPENDS roscpp std_msgs
    # DEPENDS system_lib
)
include_directories(
    # include
    ${catkin_INCLUDE_DIRS}
)
add_executable(simple_topic_publisher src/simple_topic_publisher.cpp)

add_dependencies(simple_topic_publisher ${simple_topic_publisher_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

target_link_libraries(simple_topic_publisher
    ${catkin_LIBRARIES}
)

I tried printing the the variables catkin_LIBRARIES, catkin_INCLUDE_DIRS and catkin_EXPORTED_TARGETS. They are empty.

I also tried deleting the topic_publisher_pkg files from catkin_ws/build and catkin_ws/devel/lib and rebuilding again. I still have the same error.

I also tried rebuilding “my_package” from the previous unit separately. I am having the same fatal error, knowing that it worked fine in Unit 2.

Hi @shadokku-sama ,

Welcome to this Community!

I think this is a ROS version sourcing issue. Try the following steps:

cd ~
source /opt/ros/noetic/setup.bash
cd ~/catkin_ws
rm -rf ./build ./devel
catkin_make
source devel/setup.bash
# roslaunch exercise_3.1_package launch_file.launch

Steps in detail:

  1. Go to Home directory
  2. Source ROS Noetic version
  3. Go to Catkin Workspace
  4. Remove build and devel directories
  5. Compile ROS1 packages using catkin_make
  6. Source Catkin Workspace
  7. Launch your package program

Let me know if this fixed your problem.

Regards,
Girish

Hello Girish,

Thank you for your reply.
I tried following those steps but I still get the same error as before when I compile the packages.
The catkin_* variables are still empty.

Hi @shadokku-sama ,

That is strange. Could you post the entire error output from your terminal so I can better understand your situation. I need to know what you are doing that causes this issue.

Please copy-paste all of the terminal messages on which you see this error as a text data (not as an image / screenshot).

Regards,
Girish

Here are the messages that I get:

####
#### Running command: "make -j8 -l8" in "/home/user/catkin_ws/build"
####
Scanning dependencies of target _catkin_empty_exported_target
[  0%] Built target _catkin_empty_exported_target
Scanning dependencies of target simple
Scanning dependencies of target simple_topic_publisher
[ 25%] Building CXX object topic_publisher_pkg/CMakeFiles/simple_topic_publisher.dir/src/simple_topic_publisher.cpp.o
[ 50%] Building CXX object my_package/CMakeFiles/simple.dir/src/simple.cpp.o
/home/user/catkin_ws/src/my_package/src/simple.cpp:1:10: fatal error: ros/ros.h: No such file or directory
    1 | #include <ros/ros.h>
      |          ^~~~~~~~~~~
compilation terminated.
make[2]: *** [my_package/CMakeFiles/simple.dir/build.make:63: my_package/CMakeFiles/simple.dir/src/simple.cpp.o]Error 1
/home/user/catkin_ws/src/topic_publisher_pkg/src/simple_topic_publisher.cpp:1:10: fatal error: ros/ros.h: No such file or directory
    1 | #include <ros/ros.h>
      |          ^~~~~~~~~~~
compilation terminated.
make[1]: *** [CMakeFiles/Makefile2:493: my_package/CMakeFiles/simple.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
make[2]: *** [topic_publisher_pkg/CMakeFiles/simple_topic_publisher.dir/build.make:63: topic_publisher_pkg/CMakeFiles/simple_topic_publisher.dir/src/simple_topic_publisher.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:520: topic_publisher_pkg/CMakeFiles/simple_topic_publisher.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Invoking "make -j8 -l8" failed

Hi @shadokku-sama ,

I did some cross-checking and noticed that your CMakeLists.txt file is fine. There are no issues there.
Your package.xml might be missing a dependency.

Try uncommenting only this line in your CMakeLists.txt file:

catkin_package(
    # INCLUDE_DIRS include
    # LIBRARIES topic_publisher_pkg
    CATKIN_DEPENDS roscpp std_msgs   # <--- uncomment only this line
    # DEPENDS system_lib
)

If this solution does not work, then post your package.xml file also.

Are you sure you are compiling your package(s) only inside ~/catkin_ws?

Regards,
Girish

Hi Girish,

I tried uncommenting the CATKIN_DEPENDS roscpp std_msgs line and I still get the same error.

And I confirm that I am compiling the packages only inside ~/catkin_ws.

Here are the contents of my package.xml file for topic_publisher_pkg :

<?xml version="1.0"?>
<package format="2">
  <name>topic_publisher_pkg</name>
  <version>0.0.0</version>
  <description>The topic_publisher_pkg package</description>
  <maintainer email="user@todo.todo">user</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>std_msgs</exec_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

Hi @shadokku-sama ,

Your package.xml file also looks fine. There are no issues.

Your issue seems weird, since other posts that refer to the same issue in the internet seems helpless in your case.

I just have one final method: Remove and re-create catkin_ws. Follow the steps below:

  1. Make a backup of all the packages that you have inside ~/catkin_ws/src folder. You can right-click on the src folder and choose “Download” to save the folders into your personal computer.
  2. Remove the catkin_ws directory [after taking the backups in step 1].
    cd ~
    rm -rf ./catkin_ws
    
  3. Refresh the course webpage by clicking on the refresh button on your internet browser.
  4. Create catkin_ws again:
    cd ~
    mkdir -p ./catkin_ws/src
    cd ~/catkin_ws
    catkin_make
    source devel/setup.bash
    
  5. Now copy the backed-up files that you downloaded in step 1, back into ~/catkin_ws/src in the course IDE. Right-click on src folder in the IDE and select “Upload files” and choose the file(s) from your computer.
  6. Extract the files if you downloaded them as compressed file.
  7. Once you have uploaded (and extracted) your backup files, re-run catkin_make:
    cd ~/catkin_ws
    catkin_make
    source devel/setup.bash
    

This time you should have no issues. In case you still have issues, please report here.

Regards,
Girish

Hi Girish,

Sorry for the trouble.

I removed and re-created the whole catkin_ws workspace.
Then I added the roscpp and std_msgs packages and the files they contained.

To be sure, I created the package topic_publisher_pkg using the command line $ catkin_create_pkg.

After compiling, everything was working well.

But as soon as I added the C++ file, the compiler raised the same fatal error: ros/ros.h: No such file or directory .
However, when I hover my mouse over the line #include <ros/ros.h> in the IDE, the correct path to the library /opt/ros/noetic/include/ros/ros.h is given.

Hi @shadokku-sama ,

This is definitely a ROS version sourcing issue that is glitching somewhere I am unable to diagnose.

I will tag two other admins here: @albertoezquerro and @bayodesegun .
One of them should be able to help you with this issue.

Regards,
Girish

1 Like

I don’t understand what this means. Could you please clarify?

Did you create the package with the right command and in the right folder:

cd ~/catkin_ws/src
catkin_create_pkg topic_publisher_pkg roscpp std_msgs

Also, please share the structure of your workspace showing the package, by taking a screenshot of the IDE, expanding all the folders concerned. See an example below:


Hi bayodesegun,

Thank you for your reply.

  1. Clarification about roscpp and std_msgs packages:

As Girish suggested, I downloaded the src directory before erasing catkin_ws.
When I recreated catkin_ws, I added the roscpp and std_msgs packages by hand. The ones I downloaded beforehand contained two files: CMakeLists.txt and package.xml so I added them.

  1. Command line to create topic_publisher_pkg package:

Yes I used the same command lines you mentioned.

  1. Structure of the workspace:

WOW, I see one problem already! Please remove the two folders above topic_publisher_pkg; they are definitely causing problems! Remove the folders and everything inside them.

  • roscpp
  • std_msgs

After that, recompile the workspace:

cd ~/catkin_ws
rm -rf build/ devel/
catkin_make
source devel/setup.bash

Please send another screenshot showing your catkin_ws and topic_publisher_pkg, similar to what I have in my screenshot.

If you still have problems after that

    1. Delete the package and create it again from scratch. You can make it work gust by following the hints given in the exercise. Nothing more.

I deleted the roscpp and std_msgs folders. And now it is working!

Thank you so much for your help!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.