Exercise 5.2.1 – what is the correct name of the header file?

When building the package of laser reader in exercise 5.2.1, I added the following lines to the CMakeList:

add_executable(laser_reader src/laser_reader.cpp)
target_link_libraries(laser_reader ${catkin_LIBRARIES})

Also, I’ve checked the previous discussion on the exercise 5.2.1, which suggests the revision of the path and name of the header file. While the “No such file or directory” error still occurs after changing the header file title. I am wondering what the correct name and path for should be for the header.

Any further suggestions will be highly appreciated. Thanks in advance!

Hi @super_leon,

could you paste here the entire CMakeLists.txt that you have, and also the complete error output so that we can better understand/diagnose it?

Hi, @ralves, Thank you very much for your prompt reply.

Sure. Here is the error message I got:

/home/user/catkin_ws/src/laser_reader/src/laser_reader.cpp:1:10: fatal error: laser_reader/laser_reader.h: No such file or directory
    1 | #include "laser_reader/laser_reader.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [laser_reader/CMakeFiles/laser_reader.dir/build.make:63: laser_reader/CMakeFiles/laser_reader.dir/src/laser_reader.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1926: laser_reader/CMakeFiles/laser_reader.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Invoking "make -j8 -l8" failed

And the entire CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.0.2)

find_package(catkin REQUIRED COMPONENTS

  INCLUDE_DIRS include
  LIBRARIES  laser_reader
  CATKIN_DEPENDS roscpp sensor_msgs


add_executable(laser_reader  src/laser_reader.cpp)
target_link_libraries(laser_reader  ${catkin_LIBRARIES})

Many thanks for your support!

Hi @super_leon,

thank you very much for the error logs and the CMakeLists.txt file.

If the error is:

/home/user/catkin_ws/src/laser_reader/src/laser_reader.cpp:1:10: fatal error: laser_reader/laser_reader.h: No such file or directory
    1 | #include "laser_reader/laser_reader.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Then, this means that you don’t have a file named laser_reader.sh inside a folder named laser_reader that is expected to be inside the /home/user/catkin_ws/src/laser_reader/include folder.

If you run the command below:

find  ~/ -name laser_reader.h

Do you see the path below in the output?


Since you are including laser_reader/laser_reader.h, the header file has to be inside a folder with the same name.

If your header file is inside include/laser_reader.h instead of include/laser_reader/laser_reader.h, then you could instead change the laser_reader.cpp to just #include “laser_reader.h” instead of #include “laser_reader/laser_reader.h”.

But the correct way is to just put the header file in the right place:


and then compile the workspace again.

Please let us know how it goes after the putting the file in the right place.

Hi, @ralves. Thank you very much for your help. It works now!
I tried both include/laser_reader.h and include/laser_reader/laser_reader.h before, but fortunately it works now.
By the way, I still keep the include guards as below:


should I replace them with LASER_READER_H. Sorry for my naive question. I am not very sure about this. Many thanks again!

I’m happy you finally managed to make it work, @super_leon.

Related to the #ifndef, it is good to keep the name of the header file, in this case, LASER_READER_H, just to avoid errors if you are also including in the program the laser_turtle.h

The #ifndef instructions means “if not defined”, so, “if not defined LASER_TURTLE_H, defined it by including all code until #endif

At the end of the file you have the “#endif”.

The #ifndef, #define, #endif instructions are basically to include the header file only once.

If you remove the #ifndef and try to include the same file twice, you are going to have errors because the compiler identifies that you are trying to create classes that already exist, for example.

Please let me know if you have other related questions.

Hi, @ralves. Thank you so much for your detailed explanation! I am very sorry for my late response. I gained better understanding on how to set the headers. For now I think my questions have been solved perfectly. Many thanks again!

1 Like

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