In this lesson you will see how to create a Ros2 Cplusplus package.
Please make sure that you watch the previous video on how to create a Python package so you can better
understand.
And the first thing is to navigate inside your Ros2 workspace.
So for example, from anywhere you just go back to your home directory.
You go to the roster workspace and inside the source folder okay.
Here we already have one package.
So my Python package.
And that's where we will put all the packages for our application for our workspace.
So now that you are in this source folder you can do Ros2 and then pkg create okay.
So that's the same command.
Then we're going to give a name.
So let's do my CP pkg.
So once again you can name it as you want as long as it makes sense here for this example.
And for this first package of this course I just name it my C plus plus package.
We need to provide a build type.
And this time the build type is going to be element underscore CMake.
That's going to create a C plus plus package.
Okay.
So we had element Python for Python packages.
And we have element cmake for C plus plus packages.
And as you will see the architecture of the package is going to be completely different.
So with that command that's going to be enough.
You can already launch it.
But if we want we can also add dependencies okay.
And I'm going to add a dependency this time to our CL cpp.
So we have our clcp which is the C plus plus library for ros2.
And that's going to allow you to write C plus plus code to interact directly with the functionalities
of Ros2.
And you can add as many dependencies you want after this option here.
For now we just add one.
We're going to add more later in this course.
So now that I have this command inside the source folder I can press enter.
And that's going to create another package.
So you can see we also have some logs creating folders and files.
We also have a warning for the license okay but no need to worry about that.
We don't need a license for now.
And so let's do a clear here LZ.
And you can see we have a my C plus plus package.
So let's open VSCode and well if you have closed VSCode make sure you go back again.
You go back to your source folder and then you do code dot always like that.
That's what I always do and I never have any problem.
So you press enter here and you can see we have still our Python package.
Our C plus plus package now is right here.
And the architecture is quite different.
As you can see here for a C plus plus package we have an include folder with the name.
So another folder with the name of the package.
And then we have a source folder after that.
We also have a CMake list dot txt.
So I'm just going to ignore all of the pop ups here because we already have the correct extensions in
VSCode.
And as you can see, well, if you already know a bit of C plus plus that's typically a well a C plus
plus project.
You have a source and include folder and then a cmakelists.txt.
So this is really basically a standard C plus plus architecture inside the Ros2 package.
All right.
So that's going to be the implementation files.
The CPP files in the include you have the header files.
There's going to be the HPP files or the H files.
And then in Cmakelists.txt you're going to write the rules to build your code.
Okay I'm going to come back to the Cmakelists.txt when we write the first note.
For now let's just close that.
And you see we also have a package dot XML.
And this one is very similar to the one we had for Python.
Okay.
So package dot XML.
Basically this is the only file that you need to have in every Ros2 to package.
Okay.
So that's the minimum thing you need to have.
And as you can see we also have name version description emails license.
So we have all those things.
If you want to publish the package you will need to fill those.
Then you see we have the Aim and CMake here.
That's the build type.
And we also have the dependency with the depend tag with rcl, cpp.
And so if you want to add more dependencies you will need to add more depend tag just after that one.
Great.
So now let's go back to the terminal and let's build this package.
So once again don't run call build here just in case I'm going to run call on build okay in my source
folder.
So I make the mistake of running call on build here.
What's going to happen.
Well you see it's still building stuff.
And now I have to install a build and a log folder inside my source repository.
But I also have the same thing in my ros2 workspace.
So if you have that well things can be confusing and you might have a lot of errors in the future.
So if you have made a mistake, it's not a problem you just remove.
So let's do a remove with recursive of the build, install and log.
Okay, so that's just to show you that if you build in the wrong place, make sure you remove all those
folders.
And then make sure you build exactly in this Ros2 workspace.
So now I can do call on build.
And you can see we are building the C plus plus package and the Python package.
And if I only want it to build the C plus plus package I can do call build with packages select and
then my C plus plus package.
I press enter and you can see that we only build that package okay.
So this option is very useful.
Great.
Now you have a C plus plus package.
You also have a Python package.
So let's see what a node is and then let's create one.