Targets



Resources

  • Exemplar CMake File - lots of tricks, including bundling the headers with the target so you don't need separate install commands
  • It's time to do CMake right - quick illustration of how to write a find module for a package that only has a module supporting legacy style

Imported Targets


Extracting header directories
get_target_property(FOO_INCLUDE_DIRS foo::foo INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(FOO_LIBRARIES foo::foo INTERFACE_LINK_LIBRARIES)
get_target_property(FOO_COMPILE_OPTIONS foo::foo INTERFACE_COMPILE_OPTIONS)
get_target_property(FOO_COMPILE_DEFINITIONS foo::foo INTERFACE_COMPILE_DEFINITIONS)
get_target_property(FOO_COMPILE_FEATURES foo::foo INTERFACE_COMPILE_FEATURES)

message(STATUS "FOO_INCLUDE_DIRS....${FOO_INCLUDE_DIRS}")
message(STATUS "FOO_COMPILE_OPTIONS....${FOO_COMPILE_OPTIONS}")
message(STATUS "FOO_COMPILE_DEFINITIONS....${FOO_COMPILE_DEFINITIONS}")
message(STATUS "FOO_COMPILE_FEATURES....${FOO_COMPILE_FEATURES}")
message(FATAL_ERROR "FOO_LIBRARIES....${FOO_LIBRARIES}")

See also the list of target properties.

Common Errors

INTERFACE_INCLUDE_DIRECTORIES


Problem Code
target_include_directories(
  ${PROJECT_NAME}
  PUBLIC
    $<BUILD_INTERFACE:${example_interfaces_INCLUDE_DIRS}>
    $<BUILD_INTERFACE:${muppet_msgs_INCLUDE_DIRS}>
    $<BUILD_INTERFACE:${rclcpp_INCLUDE_DIRS}>
    $<BUILD_INTERFACE:${rclcpp_action_INCLUDE_DIRS}>
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)
Console Error
CMake Error in src/lib/CMakeLists.txt:
  Target "muppet_motion_primitives" INTERFACE_INCLUDE_DIRECTORIES property
  contains path:

    "/mnt/mervin/workspaces/crystal/muppet/src/Outdoor_muppet_curb_climber/muppet_motion_primitives/src/lib/"

  which is prefixed in the source directory.


-- Generating done                                           
-- Build files have been written to: /mnt/mervin/workspaces/crystal/muppet/build/muppet_motion_primitives
--- stderr: muppet_motion_primitives
CMake Error in src/lib/CMakeLists.txt:
  Target "muppet_motion_primitives" INTERFACE_INCLUDE_DIRECTORIES property
  contains path:

    "/mnt/mervin/workspaces/crystal/muppet/src/Outdoor_muppet_curb_climber/muppet_motion_primitives/src/lib/"

  which is prefixed in the source directory.
)

Solution is to put quotes around the interfaces in case they have ';' in them (need to interpret them as strings, not decompose them further into lists).

Solution
target_include_directories(
  ${PROJECT_NAME}
  PUBLIC
    "$<BUILD_INTERFACE:${example_interfaces_INCLUDE_DIRS}>"
    "$<BUILD_INTERFACE:${muppet_msgs_INCLUDE_DIRS}>"
    "$<BUILD_INTERFACE:${rclcpp_INCLUDE_DIRS}>"
    "$<BUILD_INTERFACE:${rclcpp_action_INCLUDE_DIRS}>"
    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
    $<INSTALL_INTERFACE:include>
)