[ROS Q&A] 163 – Custom message fails to build: No such file or directory

[ROS Q&A] 163 - Custom message fails to build- No such file or directory

Written by Alberto Ezquerro

06/11/2018

Learn how to solve a common error “No such file or directory” when compiling a package that contains a custom message. This post answered the following question on ROS Answers:  https://answers.ros.org/question/306884/custom-message-fails-to-build-no-such-file-or-directory/.


Related Resources


Step 1: Get your development environment ready

Either of the following will do:

  1. Use the ROS Development Studio (ROSDS), an online platform for developing for ROS within a PC browser. Easy-peasy. I’m using this option for this post
    1. Once you log in, click on the New ROSject button to create a project that will host your code. Then Click on Open ROSject to launch the development/simulation environment.
    2. To open a “terminal” on ROSDSpick the Shell app from the Tools menu.
    3. You can find the IDE app on the Tools menu.
  2. You have ROS installed on a local PC. Okay, skip to Step 2.

Next step!

Step 2: Create a package as specified in the question to reproduce the problem

1. If you are working on ROSDS, please create a ROSject first, as indicated above.

2. Open a terminal and create a package:

user:~$ cd catkin_ws/src
user:~/catkin_ws/src$ catkin_create_pkg custom_msg rospy roscpp
Created file custom_msg/CMakeLists.txt
Created file custom_msg/package.xml
Created folder custom_msg/include/custom_msg
Created folder custom_msg/src
Successfully created files in /home/user/catkin_ws/src/custom_msg. Please adjust the values in package.xml.

3. Create a custom message

user:~/catkin_ws/src$ cd custom_msg/
user:~/catkin_ws/src/custom_msg$ mkdir -p msg
user:~/catkin_ws/src/custom_msg$ cd msg
user:~/catkin_ws/src/custom_msg/msg$ touch my_msg.msg
user:~/catkin_ws/src/custom_msg/msg$

4. Open the IDE and paste the following into my_msg.msg:

int32 my_int
float64 my_float

5. Add the following line to package.xml

<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>

6. Create a source file.

user:~/catkin_ws/src/custom_msg/msg$ cd ../src
user:~/catkin_ws/src/custom_msg/src$ touch custom_msg.cpp
user:~/catkin_ws/src/custom_msg/src$

Add the following to custom_msg.cpp using the IDE: just a reference to the custom message and an empty main function.

#include <custom_msg/my_msg.h>

int main() { return 0; }

7. Edit CMakeLists.txt as follows

– In find_package, add std_msgs and message_generation:

find_package(catkin REQUIRED COMPONENTS 
    roscpp 
    rospy
    std_msgs 
    message_generation
)

– Uncomment and replace add_message_fileswith the following:

add_message_files(
  FILES
  my_msg.msg
)

– Uncomment generate_messages and replace with:

generate_messages(DEPENDENCIES std_msgs )

– Replace catkin_package with the following:

catkin_package(CATKIN_DEPENDS rospy roscpp message_runtime)

– Add these lines under the build section to register the executable for building:

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

8. Try to build the package

user:~/catkin_ws$ catkin_make

This ends up with the following error

home/user/catkin_ws/src/custom_msg/src/custom_msg.cpp:1:31: fatal error: custom_msg/my_msg.h: No such file or directory
compilation terminated.

Done here. We are able to reproduce the error. Now let’s fix it.

Step 3: Fix the problem and rejoice!

The above error says it cannot find the header file while building the C++ source. This is because we have not specified that this header file is a dependency and should be built first.

Add the following line to the build section of CMakeLists.txt (under the lines added earlier). The format is add_dependencies(source_file_name package_name_generate_messages_cpp).

add_dependencies(custom_msg custom_msg_generate_messages_cpp)

Compile again and it should be, “voila!”:

user:~/catkin_ws$ rm -rf build/ devel/
user:~/catkin_ws$ catkin_make

I’m outta here :).

Extra: Video of the post

Here below you have a “sights and sounds” version of this post, just in case you prefer it that way. Enjoy!

Feedback

Did you like this post? Do you have any questions about the explanations? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it.


Edited by Bayode Aderinola

Topics: ROS Q&A
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This