CLion 2021.2 Help

ESP-IDF

ESP-IDF is the official development framework for the ESP32 and ESP32-S Series SoCs. This article is an introductory guide for working with ESP-IDF in CLion.

Some steps replicate the ESP-IDF Get Started guide. Examples below are given for the case of macOS, Windows-specific instructions are marked accordingly.

Prepare the environment

  1. Make sure you have a recent Python version, pip, and git installed on your system.

  2. Using your system terminal, create a directory for ESP-IDF and clone the official repository:

    mkdir -p ~/esp cd ~/esp git clone --recursive https://github.com/espressif/esp-idf.git

    ESP-IDF will be downloaded into ~/esp/esp-idf.

  3. In the same shell, install the tools:

    cd ~/esp/esp-idf ./install.sh

  4. On Windows, make sure the USB bridge of the ESP-32 board is identified correctly. You can check that in the Device Manager.

Set up a sample project using CLion's terminal

  1. In CLion, go to File | Open and select the ~/esp/esp-idf directory.

    At this point, project loading might fail due to the Current directory '../esp/esp-idf' is not buildable... error.

  2. Open CLion's built-in terminal (View | Tool Windows | Terminal or Alt+F12).

  3. Set up the environment variables by running . $HOME/esp/esp-idf/export.sh:

    The result of setting ESP-IDF environment

  4. Let's configure the hello_world project from the examples/get-started directory. The following commands set ESP32 chip as the target and run the menuconfig configuration utility:

    cd $IDF_PATH/examples/get-started/hello_world idf.py set-target esp32 idf.py menuconfig

    The last command opens the configuration menu. As the hello_world project we are using will run with default configuration, you don't need to set it up in this menu.

    Configuration menu

    Step out from the menu, and check the configuration is complete.

  5. Now we can build hello_world by running idf.py build:

    Build result

Next up, you can continue working with the project in CLion using the terminal. This will not require additional CMake configuration. Alternatively, you can set up CMake so that the project can be loaded, built and run without the command line (see below).

Configure an ESP CMake project in CLion

Toolchain on Windows

  • On Windows, go to Settings / Preferences | Build, Execution, Deployment | Toolchain and configure a toolchain for your project. See Tutorial: Configure CLion on Windows for instructions.

Open the Hello-world project

  1. Go to File | Open and select the examples/get-started/hello_world folder.

  2. Click Open as Project and trust the sources.

    At this point, project loading might fail.

Configure the environment variables

Next, set up the required environment variables.

  1. Go to Settings / Preferences | Build, Execution, Deployment | CMake and use the Environment field to add a new variable called IDF_PATH.

    Set the value to the esp-idf folder path, which you can copy from the terminal.

  2. After adding the IDF_PATH variable, CMake load might still fail due to certain Python dependencies being missed. In this case, adjust the PATH variable to include all entries pointing to the .espressif folder.

    1. Switch back to the terminal and run echo %PATH%.

    2. Copy everything that contains the .espressif folder.

    3. Navigate to CMake settings again, open the Environment field, and find PATH in the list of system variables.

    4. Prepend the value with what you have just copied. Make sure there is a semicolon at the end of the paste.

    Environment variables for CMake
  3. Call Tools | CMake| Reset Cache and Reload Project from the main menu. Project loading should perform successfully.

Build the project

  1. CLion will automatically create run/debug configurations for all the detected CMake targets.

    To build the project, select app in the configuration switcher, then click the hammer icon or press Ctrl + F9 to run build for this configuration.

  2. At this point, all CLion code insight features are available for your ESP project. Watch Developing for ESP32 With CLion on Windows: Code insight for some examples.

Flashing and monitoring an ESP-32 chip in CLion

  1. Select flash in the configuration switcher and click the hammer icon or press Ctrl + F9 to build it.

    In the build output, you can check the percentage indicator for chip flashing.

  2. Select the monitor configuration and build it. If you get an error that serial port can not be opened, then do the following:
    1. Open the Device Manager and check the port that is used by your ESP device.

    2. Add one more variable to the Environment variables in CMake settings: ESPPORT. Set the value to the port, for example, COM3.

    3. After the project is reloaded, build the monitor configuration again. The ESP-32 chip should report back as expected.

Known issues and limitations

  • There's a feature request for the ability to initialize toolchain environment via a script.

  • Currently, the size of long is considered 64-bit, while there should be 32-bit for ESP-32 (CPP-23731). To workaround this, go to File | Settings | Languages & Frameworks | C/C++ | Clangd and add --target=riscv32 to the clangd diagnostics options.

Last modified: 21 September 2021