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

Linking library with node in same package

asked 2021-02-10 23:04:04 -0600

Rufus gravatar image

updated 2021-02-10 23:08:08 -0600

I have the following in my CMakeLists.txt

project(robot)
...
add_library(${PROJECT_NAME} src/Robot.cpp)
ament_target_dependencies(${PROJECT_NAME} rclcpp)

add_executable(node src/node.cpp)
ament_target_dependencies(node ${PROJECT_NAME} ${PROJECT_NAME}_msgs rclcpp) # <--- this line

However, when compiling with colcon build, it gives the following error

ament_target_dependencies() the passed package name 'robot' was not found

for the highlighted line.

How do I let nodes use libraries that were compiled in the same package?

With ROS1 catkin, this was done with the following lines

add_dependencies(node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(node ${PROJECT_NAME} ${catkin_LIBRARIES})

which I assume is now combined into a single ament_target_dependencies()?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2021-02-12 05:27:27 -0600

Rufus gravatar image

See this example on creating a library and using it in the same package.

In particular, note that for packages that aren't coming from find_package, e.g. locally compiled libraries in the same package, you will need to use target_link_libraries. See this answer.

In additon, make sure the libraries and packages are installed in the correct location, or else the node will not be able to find the library

install(
  TARGETS my_lib
  DESTINATION lib
)

install(
  TARGETS node
  DESTINATION lib/${PROJECT_NAME}
)
edit flag offensive delete link more
0

answered 2022-03-24 22:19:12 -0600

changcong gravatar image

You can use rosidl_target_interfaces(<target> ${PROJECT_NAME} "rosidl_typesupport_cpp") instead of the add_dependencies(<target> ${PROJECT_NAME}_gencfg ${catkin_EXPORTED_TARGETS}). More details in https://docs.ros.org/en/foxy/Tutorial...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-02-10 23:04:04 -0600

Seen: 1,744 times

Last updated: Mar 24 '22