如何修复 ROS2 undefined reference to symbol 'rmw_get_serialization_format'
问题
你正在尝试使用 rclcpp 编译 C++ 可执行文件,但在链接阶段你看到如下错误消息
linker_error_example.txt
/usr/bin/ld: CMakeFiles/my_rclcpp_node.dir/main.cpp.o: undefined reference to symbol 'rmw_get_serialization_format'
/usr/bin/ld: /opt/ros/jazzy/lib/librmw_implementation.so: Fehler beim Hinzufügen von Symbolen: DSO missing from command line解决方案
此错误仅表示 librcl.so 需要一个名为 rcl_timer_call_with_info 的符号,而该符号在链接阶段未找到。换句话说,你需要链接提供此符号的库。在这种情况下,你需要使用 -lrmw_implementation 链接 librmw_implementation.so。
通常这意味着你没有正确配置构建系统。默认情况下,ROS2 期望你让 ROS2(及其 CMake 宏)处理可执行文件的链接。
但是,如果你想手动链接可执行文件,你需要在 CMakeLists.txt 中添加以下内容:
CMakeLists.txt
target_link_libraries(your_executable_name rmw_implementation)将 your_executable_name 替换为你的可执行目标名称。
如果你看到新的错误消息,如
linker_cannot_find_lrmw.txt
/usr/bin/ld: cannot find -lrmw_implementation: No such file or directory你可能需要在 CMakeLists.txt 中添加 ROS2 库的路径:
CMakeLists.txt
target_link_directories(your_executable_name PUBLIC /opt/ros/jazzy/lib/)If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow