1
$\begingroup$

Rosanswers logo

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()?


Originally posted by Rufus on ROS Answers with karma: 1083 on 2021-02-11

Post score: 1

$\endgroup$

1 Answer 1

1
$\begingroup$

Rosanswers logo

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}
)

Originally posted by Rufus with karma: 1083 on 2021-02-12

This answer was ACCEPTED on the original site

Post score: 0


$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.