how to add library in CMake with ament and add_subdirectory

asked 2021-04-10 12:24:54 -0500

kekpirat gravatar image

Hi,

I'm trying to make a publisher node that relies on a library(?) that I build at the same time using add_subdirectory() in the topmost CMake.

My current topmost CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(camera_inference)

# 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)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)

find_package(rclcpp REQUIRED)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/stereo_libtorch_disparity/thirdparty/dcn_cpp_plugin)

include_directories(${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME})
ament_export_include_directories(${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME})

add_executable(camera_ros_node src/camera_ros_node.cpp)
ament_target_dependencies(camera_ros_node rclcpp)

# dcn_v2_cuda_forward_v2 is from the add_subdirectory() above
target_link_libraries(camera_ros_node -Wl,--no-as-needed dcn_v2_cuda_forward_v2)

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

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

and in my CMakeLists.txt in the dcn_cpp_plugin folder I have added the library as such:

add_library(dcn_v2_cuda_forward_v2 SHARED vision.cpp)

When I run colcon build, it correctly builds the dcn_v2_cuda_forward_v2.so in the build/ folder, but when I try to run the node, I get the error

error while loading shared libraries: libdcn_v2_cuda_forward_v2.so: cannot open shared object file: No such file or directory

I feel I'm missing some ament command to export the library, so I tried separately to add libdcn_v2_cuda_forward_v2 to ament_target_dependencies and ament_export_target(libdcn_v2_cuda_forward_v2) without success. What's the correct way to fix this problem?

Thank you!

edit retag flag offensive close merge delete

Comments

I've run into this same issue. Have you found a solution?

msmcconnell gravatar image msmcconnell  ( 2021-10-19 16:19:28 -0500 )edit

I am having a similar problem and found this thread: https://answers.ros.org/question/3612...

which suggests that add_subdirectory simply doesn't seem to work properly with ament_cmake and everything more or less has to be in the top-level cmake file to work

user334478 gravatar image user334478  ( 2023-02-22 13:44:51 -0500 )edit