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

Include library from another package

asked 2022-09-25 05:49:36 -0600

bodka gravatar image

updated 2022-09-26 16:08:59 -0600

EDIT

i manege to solve it by adding the line to my ur_interface CMakeListt:

ament_export_dependencies(ur_rtde tf2)

now i dont need to use find_package in the test_clasess CMakeListt

-----------------------------------

Hello everyone. i am using ros2 foxy.

I have two packages, package A and package B. Package B uses package A, and package A uses some installed libraries (tf2,ur_rtde). When I import package A in package B and try to colcon build it, I get the following error:

--- stderr: test_clasess                                                                           
CMake Error at CMakeLists.txt:26 (add_executable):
  Target "test_clsass" links to target "ur_rtde::rtde" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?


CMake Error at CMakeLists.txt:26 (add_executable):
  Target "test_clsass" links to target "tf2::tf2" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

CMake Generate step failed.  Build files cannot be regenerated correctly.

whan i add to package B CMakeLists :

find_package(ur_rtde REQUIRED)
find_package(tf2 REQUIRED)

its work fine. t seems weird to me that I need to add find_package in package B, which is package A dependences. did i miss something? or its the only way to import library from another package?

package A CMakeLists:

cmake_minimum_required(VERSION 3.5)
project(ur_interface)

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

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()


include_directories(include)
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(ur_rtde REQUIRED)
find_package(tf2 REQUIRED)

# include/ur_interface/UrInterface.h
add_library(ur_interface src/UrInterface.cpp)

target_include_directories(ur_interface PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

target_link_libraries(ur_interface ur_rtde::rtde)


ament_target_dependencies(ur_interface tf2)

ament_export_targets(ur_interface HAS_LIBRARY_TARGET)
ament_export_include_directories(include)

install(
    DIRECTORY include/ur_interface
    DESTINATION include
)


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


#install(TARGETS ur_interface
#  DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

package B CMakeLists:

cmake_minimum_required(VERSION 3.5)
project(test_clasess)

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



# find dependencies
find_package(ament_cmake REQUIRED)
find_package(ur_interface REQUIRED)

add_executable(test_clsass src/test_clsass.cpp)
target_include_directories(test_clsass PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

ament_target_dependencies(test_clsass
    "ur_interface"
)

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

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)

  ament_lint_auto_find_test_dependencies()
endif()

ament_package()
edit retag flag offensive close merge delete

Comments

  1. Try including tf2_ros package as well.
  2. Use target_link_libraries(ur_interface PRIVATE ur_rtde::rtde)
ravijoshi gravatar image ravijoshi  ( 2022-09-25 06:58:01 -0600 )edit

tanks for the fast replay.

1.Where should i including tf2_ros? in the CMakeLists of Package A of B? add it as a package?

  1. if i add : Use target_link_libraries(ur_interface PRIVATE ur_rtde::rtde)

i get the error:

--- stderr: ur_interface                                                                     
CMake Error at /opt/ros/foxy/share/ament_cmake_target_dependencies/cmake/ament_target_dependencies.cmake:145 (target_link_libraries):
  The keyword signature for target_link_libraries has already been used with
  the target "ur_interface".  All uses of target_link_libraries with a target
  must be either all-keyword or all-plain.

  The uses of the keyword signature are here:

   * CMakeLists.txt:32 (target_link_libraries)

Call Stack (most recent call first):
  CMakeLists.txt:35 (ament_target_dependencies)

but ament_target_dependencies except only package names.

bodka gravatar image bodka  ( 2022-09-25 07:29:32 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-26 12:53:19 -0600

bodka gravatar image

ok i manege to arrange my CMakeLists now for A its:

cmake_minimum_required(VERSION 3.5)
project(ur_interface)

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

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()


include_directories(include)
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(ur_rtde REQUIRED)
find_package(tf2 REQUIRED)

add_library(ur_interface src/UrInterface.cpp)

target_include_directories(ur_interface  PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

target_link_libraries(ur_interface  ur_rtde::rtde tf2::tf2)


ament_export_targets(ur_interface HAS_LIBRARY_TARGET)

if i add PRIVATE to target_link_libraries i get the same error as before if i use find_package. if i dont use find_package i get the error:

fatal error: tf2/LinearMath/Quaternion.h: No such file or directory
    8 | #include <tf2/LinearMath/Quaternion.h>

in package.xml of package i only use: <depend>ur_interface</depend>. maybe i mess some dependency link?

edit flag offensive delete link more

Comments

i manege to solve it by adding the line to my ur_interface CMakeListt:

ament_export_dependencies(ur_rtde tf2)

now i dont need to use find_package in the test_clasess CMakeListt

bodka gravatar image bodka  ( 2022-09-26 16:06:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2022-09-25 05:49:36 -0600

Seen: 1,019 times

Last updated: Sep 26 '22