ament_cmake: linker cannot link shared library, even though it is in <package>_LIBRARIES
Hi everyone,
I am making a shared library using ament_cmake. Here is the CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(map_operations)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic -O3)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(PCL REQUIRED)
include_directories(include ${PCL_INCLUDE_DIRS})
#Add the library
add_library(${PROJECT_NAME} SHARED src/map_operations.cpp)
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES})
ament_target_dependencies(${PROJECT_NAME} geometry_msgs)
install(
DIRECTORY include/
DESTINATION include
)
install(
TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})
ament_package()
In another package, I am trying to consume this library and use it. I use find_package(map_operations)
in the CMakeLists.txt and <depend>map_operations</depend>
in package.xml.
I am able to build the translation units with the header files ${map_operations_INCLUDE_DIRS}
, however when I get to linking, I get a linking error:
/usr/bin/ld: cannot find -lmap_operations: No such file or directory
If I print out the contents of ${map_operations_LIBRARIES}
in CMakeLists.txt, it seems that the library is there:
message("${map_operations_LIBRARIES}")
/home/martin/dev/map_ops_ws/install/map_operations/lib/libmap_operations.so
I have tried linking it the following ways:
ament_target_dependencies(exec_name map_operations)
ament_target_dependencies(exec_name ${map_operations_LIBRARIES})
target_link_libraries(exec_name map_operations)
target_link_libraries(exec_name ${map_operations_LIBRARIES})
Does anyone have a clue of what is going on here?
Thank you
Platform
ROS 2 Humble
Ubuntu 22.04
Linux 5.19.0-32-generic
CMake 3.22.1
GCC 11.3.0