Ament best practice for sharing libraries

What’s the recommended best practice for exporting a library from a package for use in another package?

My specific use case is exporting composable nodes as a shared libraries from some packages and using those in another package that does link-time composition, but I imagine this is going to be a common use case.

From poking around in the rclcpp CMakeLists.txt, this is what I’ve come up with. I’m not 100% sure about the purpose of the ament_export_dependencies lines, and I’m also assuming that I still need to manually install include files just like libraries since this seems the logical thing.

ament_export_dependencies(ament_cmake)
ament_export_dependencies(rclcpp)
ament_export_dependencies(class_loader)

ament_export_include_directories(include)

ament_export_libraries(displayer_component)

It works, but is it correct?

1 Like

Yes, this will work. ament_export_libraries(...) is basically the equivalent to catkin_package(LIBRARIES ...).

A “better” / more modern way would be to generate a CMake file containing code to import the actual targets in downstream packages. This needs to steps:

  • install the generated file (example)
  • export the generated file (example)

The usage downstream is than also different: see example.

We only do this in very few cases yet but the goal is to use this approach in all packages.

2 Likes

it seems this answer is no longer completely valid as i get the fallowing error:
“ament_export_interfaces() is deprecated, use ament_export_targets() instead”

Yes, see this ROS answers post by @tylerweaver on why.