Table Of Contents

Previous topic

Building and installing C++ executables

Next topic

Building messages, services or actions

This Page

Building and installing C++ libraries and headers

In catkin, libraries will be installed in a common directory shared by all the packages in that entire ROS distribution. So, make sure your library names are sufficiently unique not to clash with other packages or system libraries.

For this example, suppose you have a shared library build target named your_library. On Linux, the actual file name will be something like, perhaps with a version number suffix.


Before compiling, collect all the header paths for your build dependencies:


That only needs to be done once in your CMakeLists.txt. These parameters are just examples. The include parameter is needed only if that subdirectory of your source package contains headers used to compile your programs. All your catkin package header dependencies are resolved via ${catkin_INCLUDE_DIRS}.

Other Package format 1 (legacy) pages describe how to resolve header dependencies in more detail.


To build your library add this command to your CMakeLists.txt, listing all required C++ source files, but not the headers:

add_library(your_library libsrc1.cpp libsrc2.cpp libsrc_etc.cpp)

If the list of source files is long, a CMake variable can help:

add_library(your_library ${YOUR_LIB_SOURCES})

If your library depends on libraries provided by other catkin packages, add this command:

target_link_libraries(your_library ${catkin_LIBRARIES})

If your library depends on additional non-catkin system libraries, include them in the target_link_libraries():


If the list of libraries is lengthy, you can similarly define a CMake variable for them.


In catkin, libraries are installed in a lib/ directory shared by all the packages in that entire ROS distribution. So, be careful what you name them.

Add these commands to your CMakeLists.txt, substituting all your library build target names for your_library:

catkin_package(INCLUDE_DIRS include
               LIBRARIES your_library)

install(TARGETS your_library

The runtime destination is used for .dll file on Windows which must be placed in the global bin folder.

Libraries typically provide headers defining their interfaces. Please follow standard ROS practice and place all external header files under include/your_package/:

install(DIRECTORY include/${PROJECT_NAME}/

That command installs all the files in your package’s include subtree. Place only your exported headers there. If yours is a Subversion repository, don’t forget to exclude the .svn subdirectories like this:

install(DIRECTORY include/${PROJECT_NAME}/
        PATTERN ".svn" EXCLUDE)