ROS2 `colcon build` 无法生成 hpp 文件
ROS2 `colcon build` could not generate hpp files
我按照教程:https://docs.ros.org/en/foxy/Tutorials/Custom-ROS2-Interfaces.html 创建自定义 msg 和 srv,并使用 colcon build
编译包,但是,我发现没有生成可在其他包中使用的 hpp 文件。例如,我有包 agv_interfaces
来维护自定义 msg 和 srv。我有 CMakeLists.txt
作为:
cmake_minimum_required(VERSION 3.5)
project(agv_interfaces)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
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(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/AgvJoy.msg"
"srv/JoyLimit.srv"
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
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>agv_interfaces</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="zhoudingjiang@gmail.com">taipan</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
命令:
colcon build --packages-select agv_interfaces
能够构建此包,但是,没有生成 *.hpp
文件。官方教程也没有提到这个。按照教程,我可以使用命令:
ros2 interface show agv_interfaces/msg/AgvJoy
看看我的消息是如何定义的,到目前为止一切顺利。
所以我尝试在其他包joystick
中使用这个接口,我有CMakeLists.txt
:
cmake_minimum_required(VERSION 3.5)
project(joystick)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
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(rclcpp REQUIRED)
find_package(agv_interfaces REQUIRED)
include_directories(./include/)
# --------------------------------
add_executable(
joy_node
"src/joy.cpp"
"src/joy-node.cpp"
)
ament_target_dependencies(
joy_node
rclcpp
agv_interfaces
)
install(TARGETS joy_node
DESTINATION lib/${PROJECT_NAME}
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()
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>joystick</name>
<version>0.0.0</version>
<description>AGV Joystick</description>
<maintainer email="zhoudingjiang@gmail.com">ros2-agv</maintainer>
<license>MIT</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>agv_interfaces</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
然后我使用colon build
,我看到错误:
In file included from /home/taipan/ros2-ws/src/joystick/src/joy.cpp:1:
/home/taipan/ros2-ws/src/joystick/./include/joystick/joy.h:3:10: fatal error: agv_interfaces/msg/agvjoy.hpp: No such file or directory
3 | #include "agv_interfaces/msg/agvjoy.hpp"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/joy_node.dir/build.make:63: CMakeFiles/joy_node.dir/src/joy.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
In file included from /home/taipan/ros2-ws/src/joystick/src/joy-node.cpp:1:
/home/taipan/ros2-ws/src/joystick/./include/joystick/joy.h:3:10: fatal error: agv_interfaces/msg/agvjoy.hpp: No such file or directory
3 | #include "agv_interfaces/msg/agvjoy.hpp"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/joy_node.dir/build.make:76: CMakeFiles/joy_node.dir/src/joy-node.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/joy_node.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
一些官方 ROS2 文档说我应该使用 rosidl_generator_cpp
而不是 rosidl_default_generators
,但是如何呢?
#include "agv_interfaces/agv_joy.hpp"
是解决方案。
只是补充@zhoudingjang 的回答,以防对某人有帮助,因为我花了很长时间。
在我的例子中,我有一个“custom_msgs/msg/RadarOdometry.msg”自定义消息,但在尝试包含它时,我不得不将它包含为
#include "custom_msgs/msg/radar_odometry.hpp"
显然,ros2 在编译消息时做了一个重命名的事情。
我按照教程:https://docs.ros.org/en/foxy/Tutorials/Custom-ROS2-Interfaces.html 创建自定义 msg 和 srv,并使用 colcon build
编译包,但是,我发现没有生成可在其他包中使用的 hpp 文件。例如,我有包 agv_interfaces
来维护自定义 msg 和 srv。我有 CMakeLists.txt
作为:
cmake_minimum_required(VERSION 3.5)
project(agv_interfaces)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
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(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/AgvJoy.msg"
"srv/JoyLimit.srv"
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
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>agv_interfaces</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="zhoudingjiang@gmail.com">taipan</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
命令:
colcon build --packages-select agv_interfaces
能够构建此包,但是,没有生成 *.hpp
文件。官方教程也没有提到这个。按照教程,我可以使用命令:
ros2 interface show agv_interfaces/msg/AgvJoy
看看我的消息是如何定义的,到目前为止一切顺利。
所以我尝试在其他包joystick
中使用这个接口,我有CMakeLists.txt
:
cmake_minimum_required(VERSION 3.5)
project(joystick)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
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(rclcpp REQUIRED)
find_package(agv_interfaces REQUIRED)
include_directories(./include/)
# --------------------------------
add_executable(
joy_node
"src/joy.cpp"
"src/joy-node.cpp"
)
ament_target_dependencies(
joy_node
rclcpp
agv_interfaces
)
install(TARGETS joy_node
DESTINATION lib/${PROJECT_NAME}
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()
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>joystick</name>
<version>0.0.0</version>
<description>AGV Joystick</description>
<maintainer email="zhoudingjiang@gmail.com">ros2-agv</maintainer>
<license>MIT</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>agv_interfaces</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
然后我使用colon build
,我看到错误:
In file included from /home/taipan/ros2-ws/src/joystick/src/joy.cpp:1:
/home/taipan/ros2-ws/src/joystick/./include/joystick/joy.h:3:10: fatal error: agv_interfaces/msg/agvjoy.hpp: No such file or directory
3 | #include "agv_interfaces/msg/agvjoy.hpp"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/joy_node.dir/build.make:63: CMakeFiles/joy_node.dir/src/joy.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
In file included from /home/taipan/ros2-ws/src/joystick/src/joy-node.cpp:1:
/home/taipan/ros2-ws/src/joystick/./include/joystick/joy.h:3:10: fatal error: agv_interfaces/msg/agvjoy.hpp: No such file or directory
3 | #include "agv_interfaces/msg/agvjoy.hpp"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/joy_node.dir/build.make:76: CMakeFiles/joy_node.dir/src/joy-node.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/joy_node.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
一些官方 ROS2 文档说我应该使用 rosidl_generator_cpp
而不是 rosidl_default_generators
,但是如何呢?
#include "agv_interfaces/agv_joy.hpp"
是解决方案。
只是补充@zhoudingjang 的回答,以防对某人有帮助,因为我花了很长时间。
在我的例子中,我有一个“custom_msgs/msg/RadarOdometry.msg”自定义消息,但在尝试包含它时,我不得不将它包含为
#include "custom_msgs/msg/radar_odometry.hpp"
显然,ros2 在编译消息时做了一个重命名的事情。