CMakeLists.txt for Unit 9.3

The CMakeLists.txt file is

cmake_minimum_required(VERSION 2.8.3)
project(My_Examples_pkg)

find_package(catkin REQUIRED COMPONENTS
  actionlib
  actionlib_msgs
  roscpp
)

## Generate actions in the 'action' folder
 add_action_files(
   FILES
   Name.action
 )

 generate_messages(
   DEPENDENCIES
   actionlib_msgs
 )

catkin_package(
  CATKIN_DEPENDS actionlib actionlib_msgs roscpp
)

###########
## Build ##
###########


add_executable(Example1 src/Example1.cpp)
add_dependencies(Example1 ${Example1_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(Example1
${catkin_LIBRARIES}
)

add_dependencies(Example1 my_custom_action_msg_pkg_generate_messages_cpp)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

I was wondering if we need to add the package containing the custom action to find_package()? Like this

find_package(catkin REQUIRED COMPONENTS
  my_custom_action_msg_pkg
  actionlib
  actionlib_msgs
  roscpp
)

What about catkin_package() do we need to also add my_custom_action_msg_pkg there too?

And if it is in another package do we need to add its action file in add_action_files?

Thanks in advance!

Manually modifying CMakeLists.txt and package.xml is a tricky and error-prone process that should be avoided as much as possible. Trying to remember where to modify these files is also error-prone, and is best by referring to a reference material.

The best way in this case is to add the package containing the custom action as a dependency when creating the package where you want to use it. For example:

catkin_create_pkg My_Examples_pkg roscpp my_custom_action_msg_pkg

This way, CMakeLists.txt and package.xml are automatically modified in the right places.

If it’s not practical for you to recreate the package, you can create a new dummy package with that dependency and see where the my_custom_action_msg_pkg is added in CMakeLists.txt and package.xml.