# \[Unit 3\] Exercise 3.1 topic\_publisher\_pkg: fatal error: ros/ros.h: No such file or directory

**URL:** <https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104>\
**Category:** ROS Basics In 5 Days (C++)\
**Created:** [February 27, 2024, 10:40pm UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104 "2024-02-27T22:40:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadokku-sama](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@shadokku-sama](https://get-help.theconstruct.ai/u/shadokku-sama)\
**Post date:** [February 27, 2024, 10:40pm UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/1 "2024-02-27T22:40:22Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![girishkumar.kannan](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/girishkumar.kannan/32/6994_2.png) [@girishkumar.kannan](https://get-help.theconstruct.ai/u/girishkumar.kannan)\
**Post date:** [February 28, 2024, 4:44am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/2 "2024-02-28T04:44:40Z")

</div>

Hi @shadokku-sama ,

Welcome to this Community!

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

```bash
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

---

<div class="post-metadata">

**Author:** ![shadokku-sama](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@shadokku-sama](https://get-help.theconstruct.ai/u/shadokku-sama)\
**Post date:** [February 28, 2024, 7:49am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/3 "2024-02-28T07:49:39Z")

</div>

> [@girishkumar.kannan](#):
>
> `catkin_make`

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.

---

<div class="post-metadata">

**Author:** ![girishkumar.kannan](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/girishkumar.kannan/32/6994_2.png) [@girishkumar.kannan](https://get-help.theconstruct.ai/u/girishkumar.kannan)\
**Post date:** [February 28, 2024, 8:07am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/4 "2024-02-28T08:07:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shadokku-sama](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@shadokku-sama](https://get-help.theconstruct.ai/u/shadokku-sama)\
**Post date:** [February 28, 2024, 8:25am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/5 "2024-02-28T08:25:18Z")

</div>

Here are the messages that I get:

```auto
####
#### 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

```

---

<div class="post-metadata">

**Author:** ![girishkumar.kannan](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/girishkumar.kannan/32/6994_2.png) [@girishkumar.kannan](https://get-help.theconstruct.ai/u/girishkumar.kannan)\
**Post date:** [February 28, 2024, 9:10am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/6 "2024-02-28T09:10:40Z")

</div>

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:

```bash
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

---

<div class="post-metadata">

**Author:** ![shadokku-sama](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@shadokku-sama](https://get-help.theconstruct.ai/u/shadokku-sama)\
**Post date:** [February 28, 2024, 9:54am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/7 "2024-02-28T09:54:17Z")

</div>

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 :

```auto
<?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>

```

---

<div class="post-metadata">

**Author:** ![girishkumar.kannan](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/girishkumar.kannan/32/6994_2.png) [@girishkumar.kannan](https://get-help.theconstruct.ai/u/girishkumar.kannan)\
**Post date:** [February 28, 2024, 11:01am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/8 "2024-02-28T11:01:31Z")

</div>

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].

```bash
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:

```bash
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`:

```bash
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

---

<div class="post-metadata">

**Author:** ![shadokku-sama](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@shadokku-sama](https://get-help.theconstruct.ai/u/shadokku-sama)\
**Post date:** [February 28, 2024, 1:08pm UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/9 "2024-02-28T13:08:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![girishkumar.kannan](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/girishkumar.kannan/32/6994_2.png) [@girishkumar.kannan](https://get-help.theconstruct.ai/u/girishkumar.kannan)\
**Post date:** [February 28, 2024, 1:54pm UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/10 "2024-02-28T13:54:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [February 29, 2024, 11:30am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/11 "2024-02-29T11:30:42Z")

</div>

> [@shadokku-sama](#):
>
> Then I added the `roscpp` and `std_msgs` packages and the files they contained.

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:

```auto
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:

* * *

 ![image](https://get-help.robotigniteacademy.com/uploads/default/original/2X/d/d35f6ffe64422f5cb14975af9fff0a0af567dfa6.png)

---

<div class="post-metadata">

**Author:** ![shadokku-sama](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@shadokku-sama](https://get-help.theconstruct.ai/u/shadokku-sama)\
**Post date:** [February 29, 2024, 1:10pm UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/12 "2024-02-29T13:10:29Z")

</div>

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:  
 ![Capture d’écran 2024-02-29 140856](https://get-help.robotigniteacademy.com/uploads/default/original/2X/1/16f242a431df6cf39ed18f4b2c7d370b78c74f87.png)

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [March 1, 2024, 10:42am UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/13 "2024-03-01T10:42:44Z")

</div>

> [@shadokku-sama](#):
>
> 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![shadokku-sama](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@shadokku-sama](https://get-help.theconstruct.ai/u/shadokku-sama)\
**Post date:** [March 1, 2024, 4:17pm UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/14 "2024-03-01T16:17:27Z")

</div>

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

Thank you so much for your help!

---

<div class="post-metadata">

**Author:** ![system](https://get-help.theconstruct.ai/uploads/default/original/2X/9/9d3ce8305cc0d0d9b6e07d15767a6f245c526f1b.jpeg) [@system](https://get-help.theconstruct.ai/u/system)\
**Post date:** [March 6, 2024, 4:17pm UTC](https://get-help.theconstruct.ai/t/unit-3-exercise-3-1-topic-publisher-pkg-fatal-error-ros-ros-h-no-such-file-or-directory/29104/15 "2024-03-06T16:17:43Z")

</div>

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