Ask Your Question

Ament/Colcon: Share variable between package CMakeLists

asked 2018-12-12 07:43:44 -0500

Flautz gravatar image

updated 2018-12-12 17:48:32 -0500

jayess gravatar image

I want to share a filepath variable between the CMakeLists of two packages.

When I set in the firstCMakeLists a variable using the following ways:

set(varABC "x" CACHE STRING "Description")
set( ENV{varDEF} x )

It won't appear in the second CmakeLists the following way:

find_package(packageA REQUIRED)
message(STATUS "${varABC}")
message(STATUS "${varDEF}")

Anybody knows how to do that the right way?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2018-12-12 11:58:09 -0500

Dirk Thomas gravatar image

Each package is a separate CMake project. For each a separate CMake invocation is being used. So variables set in one are not available in the other.

The ament_cmake function ament_package has an argument CONFIG_EXTRAS (see ). You can provide you own CMake file which will be included when other packages find your package with find_package().

You will find examples how to use the argument in various packages (e.g. in rclcpp).

edit flag offensive delete link more


Thanks, that seems to be the right thing!

Flautz gravatar image Flautz  ( 2018-12-12 14:34:08 -0500 )edit

Please mark the answer as correct (using the checkmark below the voting) if it answers the question. It just helps others to find answered questions in the future. Thanks.

Dirk Thomas gravatar image Dirk Thomas  ( 2018-12-12 15:22:35 -0500 )edit

answered 2019-06-27 10:57:55 -0500

bergercookie gravatar image

updated 2019-06-27 10:58:36 -0500

In my case I wanted to reuse a unittests-related file of package A in another package B. I resided to the following setup:

  1. packageA/CMakeLists.txt: ament_package(CONFIG_EXTRAS cmake/

  2. packageA/cmake/extra.cmake: get_filename_component(@PROJECT_NAME@_TEST_DIR @PROJECT_SOURCE_DIR@/test ABSOLUTE)

  3. packageB/CMakeLists.txt: find_package(packageA), include_directories(${packageA_TEST_DIR})

  4. Then just use the file that's under the test dir in your cpp/hpp files of package B: #include "filename-relative to *_TEST_DIR"
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-12-12 07:43:44 -0500

Seen: 230 times

Last updated: Jun 27 '19