Skip to content

Instantly share code, notes, and snippets.

@tylerjw
Created February 25, 2021 23:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerjw/ce3bcb94b3df7996e450dec18ee54cde to your computer and use it in GitHub Desktop.
Save tylerjw/ce3bcb94b3df7996e450dec18ee54cde to your computer and use it in GitHub Desktop.
ROS2 CMake magic for exporting Boost dependencies of library targets.

Do I need to know about this? (the problem)

If you have a line like this in your cmake:

find_package(Boost REQUIRED thread)

and later on you are creating and exporting a library:

add_library(my_library ...)
...
install(TARGETS my_library ...)
ament_export_dependencies(Boost ...)

And you've noticed that you get cmake errors complaining about missing libraries when you try to depend on your my_library in another package (or perhaps a user submits a bug about this).

How can I solve this for myself now? (the pattern)

Create a file named ConfigExtras.cmake and copy the find_package line into it for boost:

find_package(Boost REQUIRED thread)

In your CMakeLists.txt file where the find_package line was for Boost replace it with this to include the code from your new file:

include(ConfigExtras.cmake)

Then at the bottom of your CMakeLists.txt file add an argument to your call to ament_package like this:

ament_package(CONFIG_EXTRAS ConfigExtras.cmake)

Why do I need to do this? (the references)

Here is the ros-answers post that directed me towards this solution: https://answers.ros.org/question/331089/ament_export_dependenciesboost-not-working/?answer=332460#post-id-332460

Here is the documentation on the ament_package argument: https://github.com/ament/ament_cmake/blob/95cab1de1cd6bd7caad34d0122e11b2e86310ee2/ament_cmake_core/cmake/core/ament_package.cmake#L24-L32

Here is the PR where I did this in MoveIt2: moveit/moveit2#372

What can be done to make this easier in the future? (the upstream)

The current implementation of ament_export_dependencies can't handle this nicely for you.
In the future it would be nice if this was handled by whatever ament_cmake function sets up the exporting of dependencies of your package. If you feel equiped to solve this, here is where you might submit a PR. If you do please let me know so I can test it and stop telling people to do it this way.

@asperry
Copy link

asperry commented Sep 22, 2023

Link to Robotics Stack Exchange version of the ros-answers question: https://robotics.stackexchange.com/questions/93010/ament-export-dependenciesboost-not-working

Link to open issue regarding this problem: ament/ament_cmake#456

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment