ament / ament_cmake

Supporting CMake packages for working with ament
Apache License 2.0
100 stars 124 forks source link

Using ament_cmake and Vendor Libraries #551

Open wimos-ai opened 1 week ago

wimos-ai commented 1 week ago

Hello, I am trying to get the ROS2 CMake build system to work with some vendor libraries. They basically distribute a set of shared libraries and some header files. The issue I am running into is dynamic linker errors, mainly because some of the Shared Objects depend on each other. Eg:

By playing with CMake Rpath rules, I can get my executable to link to A.so on Linux, but then on loading A.so cannot find B or C even though they are all in the same install directory due to Linux dynamic library search path rules. I could in theory use something like patchelf to point A.so to my install directory but that feels hacky. Is there a better way?

mjcarroll commented 1 week ago

Could you potentially use the IMPORTED option from CMake in order for the library to be correctly registered as a cmake target?

Something like:

# Set the path to the external vendor project
set(VENDOR_PROJECT_DIR "/path/to/vendor/project")

# Import libA.so
add_library(libA SHARED IMPORTED)
set_target_properties(libA PROPERTIES
  IMPORTED_LOCATION "${VENDOR_PROJECT_DIR}/lib/libA.so"
  INTERFACE_LINK_LIBRARIES libB libC
)

# Import libB.so
add_library(libB SHARED IMPORTED)
set_target_properties(libB PROPERTIES
  IMPORTED_LOCATION "${VENDOR_PROJECT_DIR}/lib/libB.so"
)

# Import libC.so
add_library(libC SHARED IMPORTED)
set_target_properties(libC PROPERTIES
  IMPORTED_LOCATION "${VENDOR_PROJECT_DIR}/lib/libC.so"
)

Then you can use the libraries by doing:

target_link_libraries(my_executable PRIVATE libA libB libC)