构建ROS2自定义msg失败:找不到header__struct.hpp相关文件
自定义ROS2 Msg编译错误:找不到header__struct.h/.hpp文件
错误现象
执行colcon build --packages-select mbs_ros_msgs时编译失败,核心错误为找不到mbs_ros_msgs/msg/detail/header__struct.h和.hpp文件,完整错误日志如下:
root@docker-desktop:/home/ros2_ws# colcon build --packages-select mbs_ros_msgs Starting >>> mbs_ros_msgs --- stderr: mbs_ros_msgs In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsgpssync__functions.h:19, from /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsgpssync__functions.c:4: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsgpssync__struct.h:22:10: fatal error: mbs_ros_msgs/msg/detail/header__struct.h: No such file or directory 22 | #include "mbs_ros_msgs/msg/detail/header__struct.h" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsephtoa__functions.h:19, from /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsephtoa__functions.c:4: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsephtoa__struct.h:22:10: fatal error: mbs_ros_msgs/msg/detail/header__struct.h: No such file or directory 22 | #include "mbs_ros_msgs/msg/detail/header__struct.h" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_generator_c.dir/build.make:167: CMakeFiles/mbs_ros_msgs__rosidl_generator_c.dir/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsgpssync__functions.c.o] Error 1 gmake[2]: *** Waiting for unfinished jobs.... gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_generator_c.dir/build.make:153: CMakeFiles/mbs_ros_msgs__rosidl_generator_c.dir/rosidl_generator_c/mbs_ros_msgs/msg/detail/mbsephtoa__functions.c.o] Error 1 gmake[1]: *** [CMakeFiles/Makefile2:213: CMakeFiles/mbs_ros_msgs__rosidl_generator_c.dir/all] Error 2 gmake[1]: *** Waiting for unfinished jobs.... In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_cpp/mbs_ros_msgs/msg/mbsephtoa__type_support.cpp:7: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_cpp/mbs_ros_msgs/msg/detail/mbsephtoa__struct.hpp:20:10: fatal error: 'mbs_ros_msgs/msg/detail/header__struct.hpp' file not found #include "mbs_ros_msgs/msg/detail/header__struct.hpp" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_cpp/mbs_ros_msgs/msg/mbsgpssync__type_support.cpp:7: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_cpp/mbs_ros_msgs/msg/detail/mbsgpssync__struct.hpp:20:10: fatal error: 'mbs_ros_msgs/msg/detail/header__struct.hpp' file not found #include "mbs_ros_msgs/msg/detail/header__struct.hpp" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. 1 error generated. gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_typesupport_cpp.dir/build.make:122: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_cpp.dir/rosidl_typesupport_cpp/mbs_ros_msgs/msg/mbsephtoa__type_support.cpp.o] Error 1 gmake[2]: *** Waiting for unfinished jobs.... gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_typesupport_cpp.dir/build.make:136: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_cpp.dir/rosidl_typesupport_cpp/mbs_ros_msgs/msg/mbsgpssync__type_support.cpp.o] Error 1 gmake[1]: *** [CMakeFiles/Makefile2:395: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_cpp.dir/all] Error 2 In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_fastrtps_cpp/mbs_ros_msgs/msg/detail/dds_fastrtps/mbsgpssync__type_support.cpp:4: In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_fastrtps_cpp/mbs_ros_msgs/msg/detail/mbsgpssync__rosidl_typesupport_fastrtps_cpp.hpp:11: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_cpp/mbs_ros_msgs/msg/detail/mbsgpssync__struct.hpp:20:10: fatal error: 'mbs_ros_msgs/msg/detail/header__struct.hpp' file not found #include "mbs_ros_msgs/msg/detail/header__struct.hpp" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_introspection_cpp/mbs_ros_msgs/msg/detail/mbsgpssync__type_support.cpp:12: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_cpp/mbs_ros_msgs/msg/detail/mbsgpssync__struct.hpp:20:10: fatal error: 'mbs_ros_msgs/msg/detail/header__struct.hpp' file not found #include "mbs_ros_msgs/msg/detail/header__struct.hpp" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_typesupport_introspection_cpp.dir/build.make:144: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_introspection_cpp.dir/rosidl_typesupport_introspection_cpp/mbs_ros_msgs/msg/detail/mbsgpssync__type_support.cpp.o] Error 1 gmake[2]: *** Waiting for unfinished jobs.... In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_fastrtps_cpp/mbs_ros_msgs/msg/detail/dds_fastrtps/mbsephtoa__type_support.cpp:4: In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_fastrtps_cpp/mbs_ros_msgs/msg/detail/mbsephtoa__rosidl_typesupport_fastrtps_cpp.hpp:11: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_cpp/mbs_ros_msgs/msg/detail/mbsephtoa__struct.hpp:20:10: fatal error: 'mbs_ros_msgs/msg/detail/header__struct.hpp' file not found #include "mbs_ros_msgs/msg/detail/header__struct.hpp" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/ros2_ws/build/mbs_ros_msgs/rosidl_typesupport_introspection_cpp/mbs_ros_msgs/msg/detail/mbsephtoa__type_support.cpp:12: /home/ros2_ws/build/mbs_ros_msgs/rosidl_generator_cpp/mbs_ros_msgs/msg/detail/mbsephtoa__struct.hpp:20:10: fatal error: 'mbs_ros_msgs/msg/detail/header__struct.hpp' file not found #include "mbs_ros_msgs/msg/detail/header__struct.hpp" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_typesupport_introspection_cpp.dir/build.make:130: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_introspection_cpp.dir/rosidl_typesupport_introspection_cpp/mbs_ros_msgs/msg/detail/mbsephtoa__type_support.cpp.o] Error 1 gmake[1]: *** [CMakeFiles/Makefile2:369: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_introspection_cpp.dir/all] Error 2 1 error generated. gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_typesupport_fastrtps_cpp.dir/build.make:144: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_fastrtps_cpp.dir/rosidl_typesupport_fastrtps_cpp/mbs_ros_msgs/msg/detail/dds_fastrtps/mbsgpssync__type_support.cpp.o] Error 1 gmake[2]: *** Waiting for unfinished jobs.... 1 error generated. gmake[2]: *** [CMakeFiles/mbs_ros_msgs__rosidl_typesupport_fastrtps_cpp.dir/build.make:130: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_fastrtps_cpp.dir/rosidl_typesupport_fastrtps_cpp/mbs_ros_msgs/msg/detail/dds_fastrtps/mbsephtoa__type_support.cpp.o] Error 1 gmake[1]: *** [CMakeFiles/Makefile2:291: CMakeFiles/mbs_ros_msgs__rosidl_typesupport_fastrtps_cpp.dir/all] Error 2 gmake: *** [Makefile:146: all] Error 2 --- Failed <<< mbs_ros_msgs [2.07s, exited with code 2] Summary: 0 packages finished [2.16s] 1 package failed: mbs_ros_msgs 1 package had stderr output: mbs_ros_msgs
现有配置文件
CMakeLists.txt
cmake_minimum_required(VERSION 3.8) project(mbs_ros_msgs) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) find_package(std_msgs REQUIRED) find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} "msg/MBSEPHTOA.msg" "msg/MBSGPSSYNC.msg" DEPENDENCIES std_msgs ) ament_export_dependencies(rosidl_default_runtime) ament_package()
package.xml
<?xml version="1.0"?> <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> <package format="3"> <name>mbs_ros_msgs</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="gilbertogonzalez@todo.todo">gilbertogonzalez</maintainer> <license>TODO: License declaration</license> <buildtool_depend>ament_cmake</buildtool_depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <depend>std_msgs</depend> <buildtool_depend>rosidl_default_generators</buildtool_depend> <exec_depend>rosidl_default_runtime</exec_depend> <member_of_group>rosidl_interface_packages</member_of_group> <export> <build_type>ament_cmake</build_type> </export> </package>
问题分析与解决步骤
核心原因
错误日志显示生成的代码尝试从当前包mbs_ros_msgs中查找Header的结构体文件,但Header是std_msgs包中的标准类型,说明你的.msg文件中引用Header的方式错误,导致代码生成器误以为Header是当前包的自定义类型。
解决步骤
检查并修正.msg文件中的Header引用
打开MBSEPHTOA.msg和MBSGPSSYNC.msg,确认引用Header的语句为:std_msgs/Header header而非
Header header或mbs_ros_msgs/Header header。清理并重新编译
删除工作空间下的build和install目录,避免旧的生成文件干扰:rm -rf /home/ros2_ws/build /home/ros2_ws/install然后重新执行编译:
colcon build --packages-select mbs_ros_msgs额外排查点
- 确保ROS2环境变量已正确加载(执行
source /opt/ros/crystal/setup.bash,根据你的ROS2版本调整路径) - 确认.msg文件的路径和大小写与CMakeLists.txt中配置的一致(比如
msg/MBSEPHTOA.msg对应实际文件路径和名称)
- 确保ROS2环境变量已正确加载(执行
内容的提问来源于stack exchange,提问作者Gilberto Gonzalez Jr.
相关产品推荐
相关产品推荐

