ROS2 compiles an error message when compiling a project with a custom message: msg/detail/header__struct.h: No such file or directory

Project scenario:

When migrating ROS 1 projects to ROS 2, sometimes you will encounter changes and updates in message types, and message types may need to be adjusted to accommodate the new ROS 2 requirements. This article will introduce how to handle the Header field in the custom message to ensure that the project can smoothly adapt to the ROS 2 message type definition.


Problem Description

When compiling the ros2 package, it was found that a compilation error occurred when the project package introduced a custom message from ros1:

Starting >>> myrobot 
--- stderr: myrobot                               
/home/nvidia/ros2_ws/src/myrobot/src/ts_robot.cpp:5:10: fatal error: myrobot/msg/supersonic.hpp: 没有那个文件或目录
    5 | #include "myrobot/msg/supersonic.hpp" // CHANGE
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/ts_robot_node.dir/build.make:63:CMakeFiles/ts_robot_node.dir/src/ts_robot.cpp.o] 错误 1
make[2]: *** 正在等待未完成的任务....

According to the error message, roughlyfatal error: msg/detail/header__struct.h: 没有那个文件或目录


Cause Analysis:

In ROS 1, the field in the custom message Header headerusually refers to the message's timestamp, frame ID and other information. However, in ROS 2, the Header type should be std_msgs/Header. Therefore, when migrating a ROS 1 project to ROS 2, the Header field in the custom message needs to be adjusted appropriately.


solution:

Modify the message definition: Open the .msg file of the custom message, and Header headermodify the field std_msgs/Header headerto conform to the message type definition of ROS 2. For example:

std_msgs/Header header
# Add other fields here

After modification, rebuild it.

Summarize

Accommodating changes in message types is an important step when migrating a ROS 1 project to ROS 2. Handling the Header header field in custom messages, modifying it to std_msgs/Header header, and making sure to modify CMakeLists.txt and other references appropriately can help projects smoothly adapt to ROS 2's message type definitions. This simple tweak can help you more easily migrate your existing ROS 1 projects to ROS 2 and continue to enjoy its benefits and features.

I hope this blog post can help you deal with the problem of message type migration smoothly.

Guess you like

Origin blog.csdn.net/marin1993/article/details/132145014