Table Of Contents

Previous topic

Building and installing C++ libraries and headers

Next topic

Dynamic reconfiguration

This Page

Building messages, services or actions


Your package.xml must declare a <build_depend> on message_generation, and a <run_depend> on message_runtime:


Your messages services, or actions will probably include fields defined in other ROS messages, like std_msgs. Declare them like this:


Both <build_depend> and <run_depend> are recommended for message dependencies. That example assumes std_msgs is the only dependency. Be sure to mention all your message package dependencies here, and substitute them all for std_msgs in the examples that follow.

To generate actions, add actionlib_msgs as a dependency:



For CMake, find the catkin packages for message_generation and any messages, services or actions you depend on:

find_package(catkin REQUIRED COMPONENTS message_generation std_msgs)

For building actions, include actionlib_msgs among the dependencies:

find_package(catkin REQUIRED

Next, list your message definitions:

add_message_files(DIRECTORY msg

Similarly, if you have a service to generate:

add_service_files(DIRECTORY srv

To generate actions, add:

add_action_files(DIRECTORY action

Then, generate all your message, service and action targets with this command:

generate_messages(DEPENDENCIES std_msgs)

Make sure the catkin_package() command declares your message, service and action dependencies for other packages:

catkin_package(CATKIN_DEPENDS message_runtime std_msgs)

A good ROS practice is to collect related messages, services and actions into a separate package with no other API. That simplifies the package dependency graph.

However, you can provide scripts and programs with the message package. If you do, message generation targets need to be built before any programs that depend on them. Every target that directly or indirectly uses one of your message headers must declare an explicit dependency:

add_dependencies(your_program ${your_package_EXPORTED_TARGETS})

If your build target also uses message or service headers imported from other catkin packages, declare those dependencies similarly:

add_dependencies(your_program ${catkin_EXPORTED_TARGETS})

Since catkin installs message, service and action targets automatically, no extra install() commands are needed for them.