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

So, what I understand is that you want to export a library to be used by other packages, here's the answer for this. let's say you have a package called my_library that contains the library you want to export, and another package called lib_consumer which consumes that library.

The two packages structure:

my_library
   include
      the_library
           lib_header.hpp
   src
   CMakeLists.txt
consumer
   src
      consumer.cpp
   CMakeLists.txt

In the my_library package, your CMakeLists.txt should be as follows:

The following line makes you use direct inclusion in your packages' cpp files (i.e #include "lib_header.hpp")

include_directories(include/the_library)

You then add the library, the set is just for convenience if you have multiple header files, you can do the same if you have multiple cpp files. (the target name is my_lib)

set(HEADER_FILES include/the_library/lib_header.hpp)
add_library(my_lib src/lib_source.cpp ${HEADER_FILES})

This line to export the targets, in our case it's a library! (it passes the information to cmake to tell it that we want to export a library (more generally a target) to be used by some other package.

ament_export_targets(my_lib HAS_LIBRARY_TARGET)

Install the include/the_library directory to the install/include/the_library to be seen by the consumer target (we haven't talked about the consumer just yet)

install(
  DIRECTORY include/the_library
  DESTINATION include
)

Install and officially export the library, you may want to search a little bit more for this one

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

In the consumer package, your CMakeLists.txt should be as follows:

find_package(the_library REQUIRED)
add_executable(consumer src/consumer.cpp)

You then want to link the consumer executable to the library called my_lib exported by the_library package, the syntax is as follows: package_name::exported_library_name

target_link_libraries(consumer the_library::my_lib)

You then may then want to install the consumer executable to be able to ros2 run it

install(TARGETS consumer
DESTINATION lib/${PROJECT_NAME}/)

Important note: in the consumer.cpp file you'd include the library as follows:

#include "the_library/lib_header.hpp"

If you'd like direct inclusion (#include "lib_header.hpp") then you need to modify the my_library CMakeLists as follows:

install(
  TARGETS my_lib
  EXPORT my_lib
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include/the_library
)

Here's the complete code: google drive zip

Sorry for the long answer :)

So, what I understand is that you want to export a library to be used by other packages, here's the answer for this. let's say you have a package called my_library that contains the library you want to export, and another package called lib_consumer which consumes that library.

The two packages structure:

my_library
   include
      the_library
           lib_header.hpp
   src
   CMakeLists.txt
consumer
   src
      consumer.cpp
   CMakeLists.txt

In the my_library package, your CMakeLists.txt should be as follows:

The following line makes you use direct inclusion in your packages' package's (my_library src cpp files files, if you have any) (i.e #include "lib_header.hpp")

include_directories(include/the_library)

You then add the library, the set is just for convenience if you have multiple header files, you can do the same if you have multiple cpp files. (the target name is my_lib)

set(HEADER_FILES include/the_library/lib_header.hpp)
add_library(my_lib src/lib_source.cpp ${HEADER_FILES})

This line to export the targets, in our case it's a library! (it passes the information to cmake to tell it that we want to export a library (more generally a target) to be used by some other package.

ament_export_targets(my_lib HAS_LIBRARY_TARGET)

Install the include/the_library directory to the install/include/the_library to be seen by the consumer target (we haven't talked about the consumer just yet)

install(
  DIRECTORY include/the_library
  DESTINATION include
)

Install and officially export the library, you may want to search a little bit more for this one

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

In the consumer package, your CMakeLists.txt should be as follows:

find_package(the_library REQUIRED)
add_executable(consumer src/consumer.cpp)

You then want to link the consumer executable to the library called my_lib exported by the_library package, the syntax is as follows: package_name::exported_library_name

target_link_libraries(consumer the_library::my_lib)

You then may then want to install the consumer executable to be able to ros2 run it

install(TARGETS consumer
DESTINATION lib/${PROJECT_NAME}/)

Important note: in the consumer.cpp file you'd include the library as follows:

#include "the_library/lib_header.hpp"

If you'd like direct inclusion (#include "lib_header.hpp") then you need to modify the my_library CMakeLists as follows:

install(
  TARGETS my_lib
  EXPORT my_lib
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include/the_library
)

Here's the complete code: google drive zip

Sorry for the long answer :)

:)