ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

(I should preface this by saying that I stick with more of a vanilla CMake approach instead of using the ament CMake macros.)

I think the main problem is that rclcpp isn't a valid library name for target_link_libraries. You'll have to use ${rclcpp_LIBRARIES} instead, which gets evaluated to the names of the libraries exported by the rclcpp package.

You might also run into issues finding the correct headers for each package. As written, include_directories(include) just finds the headers in the current package but not the headers in the packages you get through find_package(). I think that the preferred "modern CMake" way to do this is to use target_include directories for each library and executable. This also lets you use generator expressions to find the correct include directory in different build and install configurations.

As a side note, the header files for your package should be in a sub-directory of the include directory with a name matching the package name. For example, include/rl2_logging/some_header.h.

For your rl2_logging example, these tweaks would look like this:

cmake_minimum_required(VERSION 3.5)
project(rl2_logging)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

add_library(rl2_logging src/log.cpp)

target_link_libraries(rl2_logging ${rclcpp_LIBRARIES})

target_include_directories(rl2_logging PUBLIC
  "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
  "$<INSTALL_INTERFACE:include>"
  ${rclcpp_INCLUDE_DIRS}
)

ament_target_dependencies(rl2_logging rclcpp)
ament_export_dependencies(rclcpp)
ament_export_interfaces(export_rl2_logging HAS_LIBRARY_TARGET)

install(
  DIRECTORY include/${PROJECT_NAME}/
  DESTINATION include/${PROJECT_NAME}/
)

install(
  TARGETS rl2_logging
  EXPORT export_rl2_logging
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
)

ament_package()