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

How to properly build include a small library/namespace in a ROS-Project

asked 2019-01-22 03:46:57 -0500

max11gen gravatar image

updated 2019-12-13 07:11:07 -0500

I have a ROS project consisting of several nodes each of which is built from the CMakeLists like this:

add_executable(node1 src/node1.cpp src/node1callback.cpp)  
target_link_libraries(node1 ${catkin_LIBRARIES})  
add_dependencies(node 1 my_ros_project_generate_messages_cpp)

Now I would like to write a small library (not sure if this is the right word) consisting of a .h and a .cpp file defining a namespace with a couple of functions that I use in multiple nodes.
Now my question is: How do I add this library to the CMakeLists? Do I add it in the same way even though it's not a node?

edit retag flag offensive close merge delete


An observation: please realise this is not ROS-specific, but a general CMake workflow question. add_library(..) as suggested by @Sebastian Kasperski is a regular CMake function, and the rest of the additions/changes to your CMakeLists.txt would be as well.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-22 04:03:54 -0500 )edit

@gvdhoorn You are right, sorry. I'll consider other forums the next time.

max11gen gravatar image max11gen  ( 2019-01-22 05:04:50 -0500 )edit

@max11gen: I did not make my comment to tell you to go to other fora. I merely wanted to make sure you did not assume this was something ROS-specific.

Many times I've had to explain that Catkin is essentially "some" convenience macros for CMake. Just wanted to make sure you understood that.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-22 05:11:50 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-01-22 03:59:09 -0500

Sebastian Kasperski gravatar image

Yes, you just use add_library instead of add_executable. Make sure to add your library to the target_link_libraries afterwards.

edit flag offensive delete link more

Question Tools



Asked: 2019-01-22 03:46:57 -0500

Seen: 885 times

Last updated: Dec 13 '19