As McMudro said you are missing some dependencies. To make it bit clearer:
catkin creates several files which are used as flags to determine if something has been built or not. The flags for messages are always following the same naming convention:
package_name_generate_messages_cpp
Where 'package_name' is of course the name of your package and '_generate_messages_cpp' is a fixed name for the build flag and cannot be changed. One of the problems is that if you misspell it, catkin will not complain but it also does not have any effect. Therefore you have to make sure that you always spell it right.
One example would be:
cmake_minimum_required(VERSION 2.8.3)
project(my_package)
find_package(catkin REQUIRED COMPONENTS genmsg roscpp std_msgs geometry_msgs ... message_generation)
## Generate messages in the 'msg' folder
add_message_files(
FILES
MyMessage.msg
)
generate_messages(
DEPENDENCIES
std_msgs
...
geometry_msgs
)
catkin_package(
INCLUDE_DIRS include
# LIBRARIES strands_gazing
CATKIN_DEPENDS std_msgs geometry_msgs ...
# DEPENDS system_lib
)
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 my_package_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 my_package_generate_messages_cpp)
which makes sure that the flag my_package_generate_messages_cpp
exists before trying to build foo
. Therefore it makes sure that the message headers are generated before building the binary.
Now, what if you have custom messages in other packages on which this package relies and which therefore have to be build first? You could include them in the same way in the add_dependency line but it is much more convenient to use one of catkins makros for this: ${catkin_EXPORTED_TARGETS}
which contains all the _generate_messages_cpp
flags for all the catkin packages you listed in the find_package
line. So all you have to change is:
- Add your second package to the
find_package
statement of the first one: find_package(catkin REQUIRED COMPONENTS my_other_package genmsg roscpp geometry_msgs ... message_generation)
- Add the macro to the dependencies
add_dependencies(foo my_package_generate_messages_cpp ${catkin_EXPORTED_TARGETS})
In fact it can never hurt to include this macro.
Another very common source of error is including the my_other_package_generate_messages_cpp
flag in the dependencies of my_package
(e.g. by using the catkin macro described above) and forgetting to add it in the package.xml file. If you do not add my_other_package
as a build and run dependency to the package.xml of my_package
, catkin will assume that both packages try to build the messages in my_other_package
and since you cannot have the same type of message twice it will exit with a cmake error. So in this case your package.xml file would have to look similar to this:
<?xml version="1.0"?>
<package>
<name>my_package</name>
...
<buildtool_depend>catkin</buildtool_depend>
<build_depend>my_other_package</build_depend>
<build_depend>geometry_msgs</build_depend>
<build_depend>genmsgs</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>std_msgs_msgs</build_depend>
...
<build_depend>message_generation</build_depend>
<run_depend>my_other_package</run_depend>
<run_depend>geometry_msgs</run_depend>
<run_depend>genmsgs</run_depend>
<run_depend>roscpp</run_depend>
<run_depend>std_msgs_msgs</run_depend>
...
<run_depend>message_runtime</run_depend>
<export/>
</package>
Hope that helps.