Intro
Creating a package using CMake
How to create, build, and run a C++ ROS2 package with a minimal setup.
References
- The Robotics Back-End- Create a ROS2 Cpp Package
- https://docs.ros.org/en/jazzy/Tutorials/Beginner-Client-Libraries/Creating-Your-First-ROS2-Package.html
Setup
Workspace Setup
β’ Create ROS2 workspace
mkdir -p ros2_ws/src β’ Initialize:
cd ros2_ws && colcon build β’ Source environment
source install/setup.bashConfigure VS Code
- Configure VS Code to write inside a C++ package
- Install any needed extensions (VS Code C++ Configuration)
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
package.xml<?xml version="1.0"?> <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> <package format="3"> <name>my_package</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="">email</maintainer> <license>TODO: License declaration</license> <buildtool_depend>ament_cmake</buildtool_depend> <depend>rclcpp</depend> <depend>std_msgs</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <export> <build_type>ament_cmake</build_type> </export> </package>
CmakeLists.txtcmake_minimum_required(VERSION 3.8) project(my_package) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) ament_package()
Build the Package
β’ Build workspace:
cd ~/ros2_ws && colcon buildExample
Starting >>> my_package Finished <<< my_package [2.41s] Summary: 1 package finished [2.61s]
β’ Source install:
source install/setup.bashNext: Build a node inside a ROS2 C++ package
- To actually add a node to a package, see Create a Minimal ROS Node (C++)



