How to fix ROS2 undefined reference to symbol 'rmw_get_serialization_format'
Problem
You are trying to compile a C++ executable using rclcpp
, but during the linking phase you see error messages such as
/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
Solution
This error just means that librcl.so
requires a symbol called rcl_timer_call_with_info
and that this symbol is not found during the linking phase. In other words, you need to link the library that provides this symbol. In this case, you need to link librmw_implementation.so
using -lrmw_implementation
.
Generally this means that you didnt configure your build system correctly. By default, ROS2 expects you to let ROS2 (and its CMake macros) handle the linking of your executables.
However, in case you want to manually link your executables, you need to add the following to your CMakeLists.txt
:
target_link_libraries(your_executable_name rmw_implementation)
Replace your_executable_name
with the name of your executable target.
In case you see an new error message like
/usr/bin/ld: cannot find -lrmw_implementation: No such file or directory
you might need to add the path to the ROS2 libraries to your CMakeLists.txt
:
target_link_directories(your_executable_name PUBLIC /opt/ros/jazzy/lib/)