Creating a C++ node
We are going to do exactly the same thing we did for the Python node: create a file, write the node, build, source, and run.
Make sure you have read the previous Python section as I will not repeat everything here. We will basically just see how to apply the process for a C++ node.
To create a C++ node, we first need a C++ package. We will use the my_cpp_pkg
package that we created previously.
Writing a C++ node
Let’s create a file for the node. Go to the src
directory inside the my_cpp_pkg
package and create a .
cpp
file:
$ cd ~/ros2_ws/src/my_cpp_pkg/src/ $ touch my_first_node.cpp
You could also create the file directly from your IDE and not use the terminal.
Now, if you haven’t done this previously, open your workspace with VS Code or any other IDE:
$ cd ~/ros2_ws/src/ $ code .
Open my_first_node.cpp
. Here is the minimal code to write a C++ node:
#include "rclcpp/rclcpp.hpp" class MyCustomNode : public rclcpp...