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> > >()'
```plaintext {filename="ros2_undefined_reference_error.txt"}

## 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

```plaintext {filename="CMakeLists.txt"}
rosidl_typesupport_cpp::get_message_type_support_handle
```plaintext {filename="CMakeLists.txt"}

but
```plaintext {filename="how-to-fix-ros2-undefined-reference-to-rosidl-message-type-support-t_block5.txt"}
rosidl_typesupport_cpp::get_message_type_support_handle<sensor_msgs::msg::JointState...
```plaintext {filename="index-symbols.py"}

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`](/2025/01/10/index-symbols-py-to-list-all-symbols-for-an-entire-directory-of-shared-objects/) which contains a script **to find the required libraries for any missing symbol**.

```cmake {filename="CMakeLists.txt"}
target_link_libraries(your_executable_name sensor_msgs__rosidl_typesupport_cpp)
```plaintext {filename="index-symbols.py"}

Replace `your_executable_name` with the name of your executable target.

In case you see an new error message like

```generic {filename="ros2_undefined_reference_error_2.txt"}
/usr/bin/ld: cannot find -lsensor_msgs__rosidl_typesupport_cpp: No such file or directory
```plaintext {filename="ros2_undefined_reference_error_2.txt"}

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

```cmake {filename="CMakeLists.txt"}
target_link_directories(your_executable_name PUBLIC /opt/ros/jazzy/lib/)
```plaintext {filename="CMakeLists.txt"}

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