Create a C++ package in ROS2 step by step
Atlasbot · 17/8/2026 · 10 min read
The article What is Package in ROS2? introduced the concept ament_cmake. This article goes into specific operations: creating a complete C++ package, from ros2 pkg create to running the first node with ros2 run.
Key concepts
Standing in folder src/ of the workspace, use ros2 pkg create with --build-type ament_cmake:
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake my_cpp_pkg \
--dependencies rclcpp std_msgs
The --dependencies flag isn't just a note — it automatically fills in the lines find_package() in CMakeLists.txt and <depend> in package.xml respectively, eliminating the need to manually edit these two files from the beginning.
Generated result:
my_cpp_pkg/
├── CMakeLists.txt ← compile declaration (see separate article)
├── package.xml ← declare dependency (see separate article)
├── include/my_cpp_pkg/ ← header .hpp if separate
└── src/ ← actual .cpp file
How it works
Create src/my_cpp_pkg/src/hello_node.cpp:
#include "rclcpp/rclcpp.hpp"
int main(int argc, char **argv) {
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>("hello_node");
RCLCPP_INFO(node->get_logger(), "hello_node đã khởi động");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
Unlike Python (declared in setup.py), the C++ package declares executable directly in CMakeLists.txt:
add_executable(hello_node src/hello_node.cpp)
ament_target_dependencies(hello_node rclcpp)
install(TARGETS hello_node
DESTINATION lib/${PROJECT_NAME})
ament_target_dependencies() is a utility function of ament_cmake — automatically link the correct library, correct include path for each dependency declared in find_package(), instead of having to write target_link_libraries() manually for each package.
Build and run:
cd ~/ros2_ws
colcon build --packages-select my_cpp_pkg
source install/setup.bash
ros2 run my_cpp_pkg hello_node
In short: With C++, "executable declaration" is located at
CMakeLists.txt(add_executable+install), unlike Python where this is located atsetup.py(entry_points) — this is the biggest difference between the two types of packages when it comes to actual operations.
Technical details
Parameters
Some practical requirements/parameters when building C++ packages in ROS2:
| Parameters | Value |
|---|---|
Minimum C++ standard rclcpp requires | C++14 (recommended C++17 or higher) |
| Small package build time (a few simple nodes) | Few seconds |
| Package build time depends on heavy libraries (OpenCV, PCL) | It may take a few minutes to cleanly build |
| Speed up iterative builds of | Using ccache (compile result cache) significantly reduces |
Practical applications
Choosing C++ over Python for a particular node is often based on performance requirements — driver nodes reading very high frequency sensor data (LiDAR, IMU at hundreds of Hz) or real-time control loops (PID running at kHz) are often written in C++ to avoid Python interpreter overhead and GIL (Global Interpreter Lock) limitations — while high-level coordination logic that is less sensitive to latency (e.g., node making robot behavior decisions) is often written Python provides faster development speed. Many real robotics projects mix both languages in the same workspace, with each node using the language best suited to its role — this is possible because both types of packages communicate via the same standard DDS/Topic layer, the C++ node and the Python node do not "know" or care about the other node's language.
In package ament_cmake, a package can also contain many independent executables sharing a common core library — a common pattern for hardware drivers that have both a main program and 1-2 auxiliary tools (eg: motor_driver_node runs main and motor_calibration_tool is used exclusively when editing, not running regularly) — both are in the same package, sharing the same code to read/write the driver register via add_library(), avoiding having to separate into 2 separate packages just because there are 2 different entry points.
Practical example
$ colcon build --packages-select my_cpp_pkg
Output:
Starting >>> my_cpp_pkg
Finished <<< my_cpp_pkg [4.12s]
Summary: 1 package finished [4.35s]
$ ros2 run my_cpp_pkg hello_node
Output:
[INFO] [1723000000.123456789] [hello_node]: hello_node đã khởi động
A build time of 4.12s for a simple node is typical — if building a C++ package takes tens of seconds or more for very simple code, check to see if you are accidentally rebuilding all unnecessary heavy dependencies.
Common mistakes
- Forgot
ament_target_dependencies()for newly added executable — compiled code gives linker error (undefined reference) even though#includeis correct — each newadd_executableneeds a separateament_target_dependenciesline listing all the dependencies it needs. - Forgot block
install(TARGETS ...)— package build was successful (no compilation error) butros2 runreported executable not found — because the output build file is only inbuild/, not installed (installed) ininstall/lib/<package_name>/whereros2 runsearched. - Include header in wrong path when separating
.hppseparately from — forgot to addinclude/${PROJECT_NAME}totarget_include_directoriesinCMakeLists.txtwhen there was a self-written header file placed in folderinclude/. - Do not rebuild after editing
CMakeLists.txt— changes to build configuration (adding dependencies, renaming executables) only take effect aftercolcon buildagain, not automatically applied like Python code that can use--symlink-install.
Conclusion
The process of creating a C++ package revolves around declaring executables and dependencies in CMakeLists.txt/package.xml — fundamentally different from Python packages in that it needs to be compiled before running. Mastering the ament_target_dependencies and install(TARGETS...) blocks is enough to avoid most common build/link errors. The next article in this category is about creating a Python package — a similar process but much simpler because no compilation is required.
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.