ROS2 setup tutorial
ROS2 is the newest version of ROS, Robot Operating System, which is a set of libraries and tools designed for robot applications.
This tutorial describes how to use CLion as an IDE for developing ROS2 applications built with colcon. If you are working with an older ROS distribution, which uses catkin build tools, please refer to the previous tutorial.
Instructions below show the following procedures on Windows: creating a ROS2 workspace, opening it as a compilation database project in CLion, building, running/debugging a package, adding another package, and rebuilding the workspace.
note
If you already have a workspace, jump to Build the workspace and generate a compilation database.
We will take the official Simple publisher and subscriber (C++) guide as an example.
Open a terminal in Administrator mode.
Source the ROS2 installation. In our case the command is
call C:\dev\ros2\ros2-windows\local_setup.bat
tip
If you get an RTI Connext DDS environment script not found warning, you can safely ignore it in the scope of this tutorial.
Create a directory for a new workspace and navigate into it
md \dev_ws\src cd \dev_ws\src
Create a publisher-subscriber package
ros2 pkg create --build-type ament_cmake cpp_pubsub
Follow the next steps to get the source files, CMakeLists.txt, and package.xml, up until building.
At this point, we have the following folder structure:
|_dev_ws
|_src
|_cpp_pubsub
|_include
|_src
subscriber_member_function.cpp
publisher_member_function.cpp
CMakeLists.txt
package.xml
In order to be able to open a ROS2 workspace in CLion, we will generate a JSON compilation database using the CMAKE_EXPORT_COMPILE_COMMANDS CMake flag.
This option is supported in CMake only for the Makefile and Ninja generators, so we also need to switch between the generators using -G
.
In the same terminal where you initially sourced the ROS2 installation, navigate to the workspace-level folder (dev_ws in our case) and run
colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G Ninja
tip
If you get the Generator: execution of make failed error, try initializing the Visual C++ environment before building. Customize the path in the following command as required:
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64
Then run the build command again.
When build is finished, make sure that dev_ws\build contains a compile_commands.json file. In case it is not there, make sure your colcon version supports generating workspace-level compilation databases (see this issue).
In the same terminal, source the workspace setup files:
call install/setup.bat
Next, launch CLion from the same shell. For more information, refer to Command-line interface.
For example, in the case of using Toolbox, you can create a script for launching the IDE. In our case, it is located in C:\IDE_shell_scripts.
cd C:\IDE_shell_scripts\ clion
As the result, CLion will be launched with the ROS2 environment already prepared.
In CLion, call File | Open from the main menu and select the compile_commands.json file in the top-level build directory:
Click Open as project:
Check that the project is loaded successfully:
By default, CLions considers the directory containing the compile_commands.json file as project root. In our case, it is the build directory. In the Project tree, the actual source files are marked as external:
To get the correct project structure, we need to set the project root to the actual workspace directory.
Call Tools | Compilation Database | Change Project Root from the main menu and select the workspace directory (dev_ws in our case).
Check the changes in Project view:
At this point, all the CLion editing features are fully available for the workspace sources.
tip
By default, CLion doesn't reload projects automatically on changes in compile_command.json except for the cases of external events like VCS update. You can change this behavior in Settings | Build, Execution, Deployment | Build Tools.
In order to build and then launch a package inside CLion, we will create a custom build target for CMake commands actually performed during colcon build
, and then create a custom application configuration for that target.
Navigate to the build logs directory. In our case, it's C:
\dev_ws . Open the command.txt file from the latest build.\log \latest_build \cpp_pubsub Copy the commands into another file and modify them to the following:
"C:\Program Files\CMake\bin\cmake.EXE" C:\dev_ws\src\cpp_pubsub -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G Ninja -DCMAKE_INSTALL_PREFIX=C:\dev_ws\install\cpp_pubsub "C:\Program Files\CMake\bin\cmake.EXE" --build C:\dev_ws\build\cpp_pubsub -- -j8 -l8 "C:\Program Files\CMake\bin\cmake.EXE" --install C:\dev_ws\build\cpp_pubsub
tip
This script will be called from the package build directory, build/cpp_pubsub, and the compilation database file will be generated there. For consistency, you can copy that file up to the workspace build level by adding the following command at the end:
copy C:\dev_ws\build\cpp_pubsub\compile_commands.json C:\dev_ws\build
Save the file as a .bat script. In our example, it is called cmake_commands.bat and placed into c:
\dev_ws .\src \cpp_pubsub\
In CLion, go to Settings | Build, Execution, Deployment | Custom Build Targets and click to add a new target.
Set the Toolchain to Visual Studio. For more information about configuring a Visual Studio toolchain in CLion, refer to Windows tutorial.
Click the three dots icon next to the Build field. Then click to add an external.
In the Program field, select the newly created script. Set the Working directory to the package build directory.
Save the custom target.
To be able to build and then launch the target we have for the package, we need to create a corresponding configuration.
In the main menu, go to Run | Edit Configurations, click and select Custom Build Application.
In the configuration settings, select the Target and make sure to remove Build from the Before launch area.
Select the configuration in the toolbar switcher:
Click the hammer icon or press Ctrl + F9. Alternatively, call Build | Build 'Build cpp_pubsub' from the main menu.
Check the result in the Run tool window.
tip
If you have added or removed files in the package, the compilation database will be regenerated accordingly. Make sure to reload it or configure auto-reload.
Open the Edit Configurations dialog again.
Modify the configuration for build to the following:
Set the Executable to the actual package binary.
Select the Run with Administrator privileges checkbox. See Debug as root for more information.
After saving the configuration, it is ready to be Run or Debugged. All the CLion debugging and dynamic analysis features will be available for the workspace code.
CLion will take the debugger from target's toolchain. In our case, it is MSVC LLDB.
tip
Another alternative for debugging is to launch a package outside CLion and then attach the debugger to a running process.
Now let’s take a look at how we can add a new package and rebuild the entire workspace in CLion. To illustrate this, we’ll take an example of adding a Simple service and client package to our dev_ws workspace.
To (re)build the entire workspace, we will configure an external tool for colcon build
and call it without leaving CLion.
Go to Settings | Tools | External Tools and click to add a new tool.
Configure the tool to perform the same
colcon build
command as we used when building from the command line:note
Make sure to set the Working directory.
After saving the tool, call it via Tools | External Tools in the main menu.
Check the results in the Run tool window:
tip
Next time use the button in the Run tool window to rebuild the workspace again.
Check that compile_commands.json has been regenerated and now includes the entities for both packages:
Make sure to reload the compilation database (press or call Tools | Compilation Database | Reload Compilation Database Project form the main menu). To avoid reloading the project manually, configure auto-reload.
At this point, both of the packages are included in CLion project model and you can work with them as usual. To build and run the second package, follow the steps described above.