How to fix ROS2 undefined reference to symbol rcl_timer_call_with_info

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 'rcl_timer_call_with_info'
/usr/bin/ld: /opt/ros/jazzy/lib/librcl.so: error adding symbols: 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 librcl.so using -lrcl.

Note that the error message itself tells you what to link: /opt/ros/jazzy/lib/librcl.so: DSO missing from command line, so you immediately know what to link.

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 rcl)

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 -lrcl: 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/)