Atlas Robotics
ROS2 PackageIntermediate

CMakeLists.txt in a ROS2 package — understanding each line

Atlasbot · 17/8/2026 · 11 min read

CMakeLists.txt in a ROS2 package — understanding each line
Standard structure of a CMakeLists.txt in package ament_cmake

Opening CMakeLists.txt of any package ament_cmake for the first time is often confusing because of many strange command lines. In fact, most packages only repeat 5-6 blocks of commands in a fixed order — understanding this pattern means being able to read, understand (and edit) most of the CMakeLists.txt in the ROS2 ecosystem.

Key concepts

cmake_minimum_required(VERSION 3.8)
project(my_cpp_pkg)

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

add_executable(hello_node src/hello_node.cpp)
ament_target_dependencies(hello_node rclcpp std_msgs)

install(TARGETS hello_node
  DESTINATION lib/${PROJECT_NAME})

ament_package()
  • cmake_minimum_required / project() — declare project name, required for all standard CMake files, not just ROS2
  • find_package(... REQUIRED) — finds and loads each dependency declared in package.xml; If this line is missing for any dependency, the build will report a "package not found" error even if that package is installed on the
  • add_executable() — declares a file .cpp to be compiled into binary, named executable (name used with ros2 run)
  • ament_target_dependencies() — automatically links the correct library + include path for the executable just declared, based on find_package() above — replaces pure CMake target_link_libraries()/target_include_directories() that you have to write yourself if you don't use ament_cmake
  • install() — declare where to copy the resulting file to folder install/ of the workspace, so that ros2 run/ros2 launch can find
  • ament_package() — is always the last line, register the package in the ament system (generate necessary files for another package find_package() to get it)

In short: The fixed order is find_package (what to declare) → add_executable (what to compile) → ament_target_dependencies (what to declare) → install (where to install) → ament_package (final). The wrong order — for example, install() an executable that has not been added_executable() — will result in a CMake error as soon as you build.

How it works

A package with many nodes only needs to repeat the pair add_executable + ament_target_dependencies + list more in install(TARGETS ...):

add_executable(node_a src/node_a.cpp)
ament_target_dependencies(node_a rclcpp)

add_executable(node_b src/node_b.cpp)
ament_target_dependencies(node_b rclcpp std_msgs)

install(TARGETS node_a node_b
  DESTINATION lib/${PROJECT_NAME})

If there is code shared between many nodes (for example a helper class), separate it into a separate library with add_library() and then add each executable link, avoid compiling the same code multiple times:

add_library(my_helper_lib src/helper.cpp)
ament_target_dependencies(my_helper_lib rclcpp)

add_executable(node_a src/node_a.cpp)
target_link_libraries(node_a my_helper_lib)

Technical details

Parameters

Some other ament_cmake macros that are less common but important when your package is reused by another package:

MacroWhen to use
cmake_minimum_required(VERSION 3.8)Current ROS2 minimum CMake version requires
ament_export_targets()Allow other packages find_package() and use your library
ament_export_dependencies()Automatically pull in your dependencies when another package uses your library
ament_export_include_directories()Tells other packages where your header is

Practical applications

When a package contains a library shared by many other packages in the robot system (eg: a package robot_utils contains geometric/mathematical functions reused in both the control node and the sensor processing node), an additional set of ament_export_* is needed to "export" properly - without this step, another package, even if find_package(robot_utils REQUIRED) is successful, still cannot link to the internal library, because ament don't know what to export. This is the difference between a package "for internal use only" and a package "designed for reuse" throughout the system.

For large packages with many .cpp files, instead of listing each file in add_executable(), often use CMake variables to compile a more concise list of source files:

file(GLOB SOURCES "src/*.cpp")
add_executable(main_node ${SOURCES})

This method is convenient when the package is growing rapidly (adding new .cpp files frequently), but it should be noted that file(GLOB ...) only scans the list of files at the time of running cmake, does not automatically detect new files added between builds — if adding a new .cpp file but the build does not recognize it, you need to run it again cmake from the beginning (rm -rf build && colcon build) instead of just the usual colcon build.

Practical example

$ colcon build --packages-select my_cpp_pkg
Output when find_package(std_msgs REQUIRED) is missing even though the code has #include <std_msgs/...>:

CMake Error at CMakeLists.txt:9 (ament_target_dependencies):
  Could not find a package configuration file provided by "std_msgs"

-- Configuring incomplete, errors occurred!

The error message points directly to the correct line and the correct missing package — reading line CMake Error at CMakeLists.txt:X carefully always shows the exact location that needs to be fixed, instead of having to guess in the entire file.

Common mistakes

SymptomsCause
Could not find a package configuration file for "X"Missing find_package(X REQUIRED), or package X not installed/sourced correctly workspace
undefined reference to ... khi linkMissing library name in ament_target_dependencies() even though find_package()
ros2 run reports no executable even though the build was "successful"Missing that executable in install(TARGETS ...)
Other packages cannot link your library even though find_package() is successfulMissing ament_export_targets()/ament_export_dependencies()

Conclusion

CMakeLists.txt in ament_cmake follows a fixed pattern — understand the correct order find_package → add_executable/add_library → ament_target_dependencies → install → ament_package to read/edit most C++ packages in the ROS2 ecosystem, including packages that have never been seen before. The next article in the section talks about package.xml — the dependency declaration file goes hand in hand with CMakeLists.txt, deciding the build order of the entire workspace.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

CMakeLists.txt in a ROS2 package — understanding each line — Atlas Robotics