Create a ROS 2 package (C++)

Software in ROS 2 is organized in packages. The goal of these packages is to provide useful functionality. Take into account that the packages should be functionality, but not too much than other software can use it. A ROS 2 package is simply a directory.

This tutorial show how to create a ROS 2 package in C++. For considering a valid package must meet a few requirements

  • The package should contain a file named package.xml
    • Provides meta information about the package
  • The package should contain a file named CMakeLists.txt
    • Provide information about the package and dependencies

A ROS 2 package have the next structure:



cd ~/ros2_ws/src/ros2
mkdir testpackagecpp
cd testpackagecpp

Checkout the name label. Should be named as the folder

cmake_minimum_required(VERSION 2.8.3)


if(NOT WIN32)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra")

find_package(ament_cmake REQUIRED)
find_package(example_interfaces REQUIRED)
find_package(rcl REQUIRED)
find_package(rmw REQUIRED)
#find_package(rmw_implementation REQUIRED)
#force opensplice implementation
find_package(rmw_opensplice_cpp REQUIRED)
#find_package(rmw_fastrtps_cpp REQUIRED)


Checkout the name label. Should be named as the folder

<?xml version="1.0"?>
<package format="2">
  <description>Package containing examples of how to use the rcl API.</description>
  <maintainer email="">Alejandro Hernandez</maintainer>
  <license>Apache License 2.0</license>






With this command you are going to build only the package: testpackagecpp

cd ~/ros2_ws
ament build --symlink-install --only-package testpackagecpp

Do you need more computing capacity in your robots? Stay tuned