# 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.

{% hint style="info" %}
OS: Ubuntu 22.04, ROS: Humble, IDE: CLion
{% endhint %}

## 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

```
user:~/workspace/ros-projects/ros2_ws_tutorial_creating_ws$ tree -L 2
.
├── build
├── cmake-build-debug
├── install
├── log
├── src
    ├── cpp_pubsub
    ├── cpp_srvcli
    └── tutorial_interfaces
```

In the workspace `ros2_ws_tutorial_creating_ws`, there are three pakcages and each of them has a CMakeLists.txt file:

```
user:~/workspace/ros-projects/ros2_ws_tutorial_creating_ws/src$ tree -L 2
.
├── cpp_pubsub
│   ├── cmake-build-debug
│   ├── CMakeLists.txt
│   ├── include
│   ├── package.xml
│   └── src
├── cpp_srvcli
│   ├── cmake-build-debug
│   ├── CMakeLists.txt
│   ├── include
│   ├── package.xml
│   └── src
└── tutorial_interfaces
    ├── CMakeLists.txt
    ├── include
    ├── msg
    ├── package.xml
    ├── src
    └── srv

```

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:

```
# Location of this file is ~/workspace/ros-projects/ros2_ws_tutorial_creating_ws/tmp/CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(ros2_ws_tutorial_creating_ws)

add_subdirectory("../src/cpp_pubsub" "../src/cpp_pubsub")
add_subdirectory("../src/cpp_srvcli" "../src/cpp_srvcli")
add_subdirectory("../src/tutorial_interfaces" "../src/tutorial_interfaces")

```

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.

<figure><img src="/files/ONiDdYE9Hh3rh2UDjtvs" alt=""><figcaption></figcaption></figure>

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.learnros2.com/ros/ide-and-cmake-setup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
