Intro

Creating a package using CMake

How to create, build, and run a C++ ROS2 package with a minimal setup.

References

Setup

Workspace Setup

β€’ Create ROS2 workspace

mkdir -p ros2_ws/src  

β€’ Initialize:

cd ros2_ws && colcon build  

β€’ Source environment

source install/setup.bash

Configure VS Code


Create a Package

β€’ Navigate to src:

cd ~/ros2_ws/src

β€’ Create a ROS2 package my_package

ros2 pkg create --build-type ament_cmake my_package --dependencies rclcpp std_msgs

This creates a ROS2 package skeleton, registers dependencies (rclcpp, std_msgs), sets up build system (ament_cmake), but does not create nodes or executables

Package Structure:

ROS2 CMake Packages

my_package/  
β”œβ”€β”€ CMakeLists.txt      # Sets rules to build the package so you can run ros2 run... 
β”œβ”€β”€ include/  
β”‚   └── my_package/     # Directory containing the public headers for the package  
β”œβ”€β”€ package.xml         # File containing meta-information about the package  
└── src/                # Directory containing the source code (AKA ROS nodes) for the package  


Build the Package

β€’ Build workspace:

cd ~/ros2_ws && colcon build

Example

Starting >>> my_package
Finished <<< my_package [2.41s]                  

Summary: 1 package finished [2.61s]

β€’ Source install:

source install/setup.bash

Next: Build a node inside a ROS2 C++ package