如何修复 ROS2 undefined reference to symbol rcl_timer_call_with_info
问题
你正在尝试使用 rclcpp 编译 C++ 可执行文件,但在链接阶段你看到如下错误消息
linker-error.txt
/usr/bin/ld: CMakeFiles/my_rclcpp_node.dir/main.cpp.o: undefined reference to symbol 'rcl_timer_call_with_info'
/usr/bin/ld: /opt/ros/jazzy/lib/librcl.so: error adding symbols: DSO missing from command line解决方案
此错误仅表示 librcl.so 需要一个名为 rcl_timer_call_with_info 的符号,而该符号在链接阶段未找到。换句话说,你需要链接提供此符号的库。在这种情况下,你需要使用 -lrcl 链接 librcl.so。
注意,错误消息本身告诉你链接什么:/opt/ros/jazzy/lib/librcl.so:DSO missing from command line,所以你立即知道要链接什么。
通常这意味着你没有正确配置构建系统。默认情况下,ROS2 期望你让 ROS2(及其 CMake 宏)处理可执行文件的链接。
但是,如果你想手动链接可执行文件,你需要在 CMakeLists.txt 中添加以下内容:
CMakeLists-link-rcl.patch
target_link_libraries(your_executable_name rcl)将 your_executable_name 替换为你的可执行目标名称。
如果你看到新的错误消息,如
ld-cannot-find-lrcl.txt
/usr/bin/ld: cannot find -lrcl: No such file or directory你可能需要在 CMakeLists.txt 中添加 ROS2 库的路径:
CMakeLists-add-libdir.patch
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