如何修复 ROS2 undefined reference to symbol '_ZN10tracetools6detail15demangle_symbolEPKc'
问题
你正在尝试使用 rclcpp 编译 C++ 可执行文件,但在链接阶段你看到如下错误消息
linker_error_example.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解决方案
此错误仅表示你的应用程序需要一个名为 tracetools::details::demangle_symbol() 的符号,而该符号在链接阶段未找到。换句话说,你需要链接提供此符号的库。在这种情况下,你需要使用 -ltracetools 链接 libtracetools.so。
通常这意味着你没有正确配置构建系统。默认情况下,ROS2 期望你让 ROS2(及其 CMake 宏)处理可执行文件的链接。
查看我们关于 index-symbols.py 的文章,其中包含一个脚本用于查找任何缺失符号所需的库。
但是,如果你想手动链接可执行文件,你需要在 CMakeLists.txt 中添加以下内容:
CMakeLists.txt
target_link_libraries(your_executable_name tracetools)将 your_executable_name 替换为你的可执行目标名称。
如果你看到新的错误消息,如
linker_error_cannot_find_ltracetools.txt
/usr/bin/ld: cannot find -ltracetools: No such file or directory你可能需要在 CMakeLists.txt 中添加 ROS2 库的路径:
CMakeLists_target_link_directories.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