Search code examples
cmakeros

How to setup CMakeLists and package.xml for generated message dependencies?


We have multiple nodes using message_generation to generate .h message files for other nodes to utilize. What is the proper way to include the generated messages in the CMakeLists and package.xml of the non-generating packages?

Currently, I am doing the following, but I always fail my build the first time then have to rebuild and it works the second time. Like it doesn't realize it has to generate the messages first. Is it maybe something set up wrong in the package_containing_generated_messages package instead?


CMakeLists.txt:

find_package(catkin REQUIRED 
    roscpp
    package_containing_generated_messages
)

catkin_package(CATKIN_DEPENDS 
    roscpp 
    package_containing_generated_messages  #I don't think this one is needed, is it?
)

package.xml:

<build_depend>package_containing_generated_messages</build_depend>
<exec_depend>package_containing_generated_messages</exec_depend>

Solution

  • Almost correct, but you are missing one crucial dependency. Here is an example based on your snippet building the node foo with your own messages (but do not forget that the dependencies in the package.xml are crucial as well, but you have them correct anyway.):

    cmake_minimum_required(VERSION 2.8.3)
    project(foo_package)
    
    find_package(catkin REQUIRED 
        roscpp
        package_containing_generated_messages
    )
    
    catkin_package(CATKIN_DEPENDS 
        roscpp 
        package_containing_generated_messages
    )
    
    include_directories(include
      ${catkin_INCLUDE_DIRS}
    )
    
    ## Declare a cpp executable
    add_executable(foo foo.cpp)
    
    ## Add cmake target dependencies of the executable/library
    ## as an example, message headers may need to be generated before nodes
    add_dependencies(foo package_containing_generated_messages_generate_messages_cpp)
    
    ## Specify libraries to link a library or executable target against
    target_link_libraries(foo
      ${catkin_LIBRARIES}
    )
    

    Where the important line is this one add_dependencies(foo package_containing_generated_messages_generate_messages_cpp) which makes sure that the flag package_containing_generated_messages_generate_messages_cpp exists before trying to build foo. Therefore it makes sure that the message headers are generated before building the binary. Note that ROS adds a suffix to your package names like <my_msgs_package_name>_generate_messages_cpp. Here is some reference.