Hey everyone 👋
In my previous blog, I shared how I started exploring ROS2 Humble using Turtlesim.
Now, I’ve moved one step ahead — by creating my own C++ node that publishes data inside ROS2.
This post will walk you through everything I did — step by step — so you can do the same on your system.
⚙️ Step 1:
Creating a New Package
- Inside your workspace:
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake pkg_2
What this does:
- Creates a folder named pkg_2
- Generates the necessary files: CMakeLists.txt, package.xml, include/, and src/
- Sets up a C++ ROS2 package ready for your node
Your structure should look like:
pkg_2/
├── CMakeLists.txt
├── include/pkg_2/
├── package.xml
└── src/
🧩 Step 2:
Writing the Node
- Create a file:
cd pkg_2/src
nano my_node.cpp
This node doesn’t publish yet — but it’s alive and running, ready to communicate.
📄 Step 3:
Editing
CMakeLists.txt
This tells ROS2 how to compile your C++ node and link it with the rclcpp library
📦 Step 4:
Updating package.xml
Open:
nano ../package.xml
And add your dependency to xml file:
rclcpp
🏗️ Step 5:
Building the Package
- Go back to your workspace:
cd ~/ros2_ws
colcon build
source install/setup.bash
colcon is the build tool front-end. It discovers packages in src/, figures out build order from package manifests, and runs the configured build tool (eg. ament_cmake) for each package. Results go into build/, install/, and log/.
Discovery
: colcon scans src/ for packages (reads package.xml).
▶️ Step 6:
Running the Node
Run it with:
ros2 run pkg_2 my_node
Output:
[INFO] [my_cpp_node]: Hello guys this is nagu !
🎉 Congratulations — you’ve just created your first C++ ROS2 node!
Typical errors you might encounter and what they mean
Package
'pkg_2'not found:
You didn’tsource install/setup.bashof your workspace (or you built under a different overlay).
Linker errors (undefined references):
you forgot toament_target_dependencies(... rclcpp)orfind_package(rclcpp).
Runtime errors about DDS:
rclcpp::initcan fail if the rmw implementation is missing or environment variables are wrong. Also firewall settings can block DDS discovery.
It appears that the
colconcommand is not currently installed on your system. This tool is required to build ROS2 workspaces. You may install it using the following commands:
sudo apt update
sudo apt install python3-colcon-common-extensions -y
💬 What’s Next?
In my next blog, I’ll extend this node to publish messages at regular intervals and create a subscriber node to receive them — this is where true ROS2 communication begins.
Stay tuned 🚀
Until then, keep experimenting and exploring the power of ROS2!
🧩 Bonus Tip:
To see all running nodes:
ros2 node list
To check logs:
ros2 topic echo /rosout
🪶 >Written by NARASIMHA ALIAS Nagu
💡 “Don’t prove, just improve.”
follow me for more knowledge