How to fix ROS2 undefined reference to rosidl_message_type_support_t

Problem

You are trying to compile a C++ executable using rclcpp, but during the linking phase you see error messages such as

ros2_undefined_reference_error.txt
/usr/bin/ld: CMakeFiles/my_rclcpp_node.dir/main.cpp.o: in function `rclcpp::Serialization<sensor_msgs::msg::JointState_<std::allocator<void> > >::Serialization()':
main.cpp:(.text._ZN6rclcpp13SerializationIN11sensor_msgs3msg11JointState_ISaIvEEEEC2Ev[_ZN6rclcpp13SerializationIN11sensor_msgs3msg11JointState_ISaIvEEEEC5Ev]+0x16): undefined reference to `rosidl_message_type_support_t const* rosidl_typesupport_cpp::get_message_type_support_handle<sensor_msgs::msg::JointState_<std::allocator<void> > >()'

Solution

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 in order to link the required library.

Note that the required library is specific to the message you’re using, so it’s not just

example.txt
rosidl_typesupport_cpp::get_message_type_support_handle

but

example.txt
rosidl_typesupport_cpp::get_message_type_support_handle<sensor_msgs::msg::JointState...

however, in general you can remember to link the library for the message type from the error message plus __rosidl_typesupport_cpp.

In this case the message is from sensor_msgs, so the library name is sensor_msgs__rosidl_typesupport_cpp.

Check out our post about index-symbols.py which contains a script to find the required libraries for any missing symbol.

example.txt
target_link_libraries(your_executable_name sensor_msgs__rosidl_typesupport_cpp)

Replace your_executable_name with the name of your executable target.

In case you see an new error message like

example.txt
/usr/bin/ld: cannot find -lsensor_msgs__rosidl_typesupport_cpp: No such file or directory

you might need to add the path to the ROS2 libraries to your CMakeLists.txt:

example.txt
target_link_directories(your_executable_name PUBLIC /opt/ros/jazzy/lib/)

Check out similar posts by category: ROS, C/C++