IDE and CMake Setup
ROS2 has its own build system and tools. For example, ROS2 humble uses ament cmake. It's convenient to use the build tools in terminal but this can cause some issues with IDE. In this article, we address some common issues.
CMake Project Setup
Let's first talk about the issues. In CLion, it only support single-project configuration. Here, project roughly means ROS packages. Recall that ROS2 development is centered around workspace and each workspace contains multiple packages. The structure below is a typical layout of a ROS2 workspace
In the workspace ros2_ws_tutorial_creating_ws
, there are three pakcages and each of them has a CMakeLists.txt file:
Here comes our first problem. We cannot just open the ros2_ws_tutorial_creating_ws
as the root directory because CLion does not support multiple projects. To make it work, we need to combine all ROS2 packages/projects as one single CMake project. This means we need to have a top-level CMakeLists.txt file. We cannot have one directly in the ros2_ws_tutorial_creating_ws
directory because it will interfere withe ROS2 build tools. What we can do is to create a tmp
directory under ros2_ws_tutorial_creating_ws
and put a CMakeLists.txt in it. For instance:
Remember to reload the CMakeLists.txt file after the change.
Resolve Symbols
You may notice that jump to definition does not work for ROS2 defined symbols. The problem is that CLion does not know where to search for the symbols. In fact, when you reloade the CMake project in CLion, it may complain that it cannot find the ament_cmake.
The solution to this problem is to add environment variables to the cmake configuration in CLion.
How do we know which variables to add? Recall that when we launch a new terminal, we need to source the ROS2 setup.bash file (i.e. /opt/ros/humble/setup.bash). To determine the environment variables, you could following the steps below:
Update ~/.bashrc file and make sure the terminal does not automatically source the ROS2 setup file.
Launch a new terminal and save the current environment variables using the command
printenv
Source the ROS2 setup file and save the environment variables again to a different file
Compare the two files using the command
diff --color -u old-env-vars-file new-env-vars-file
After the change, don't forget to reload the CMakeLists.txt files.
TODO: Set up IDE for python files:
/opt/ros/humble/lib/python3.10
Last updated