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.
Make sure you have a recent Python version, pip, and git installed on your system.
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 In the same shell, install the tools:
cd esp-idf ./install.sh
On Windows, make sure the USB bridge of the ESP-32 board is identified correctly. You can check that in the Device Manager.
tip
Watch Developing for ESP32 With CLion on Windows: Preparation for video instructions.
In CLion, go to File | Open in the main menu and select the ~
/esp directory ./esp-idf At this point, project loading might fail due to the Current directory '../esp/esp-idf' is not buildable... error.
Open CLion's built-in terminal (View | Tool Windows | Terminal or AltF12).
Set up the environment variables by running
. $HOME/esp/esp-idf/export.sh
: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.
Step out from the menu, and check the configuration is complete.
Now we can build hello_world by running
idf.py build
:
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).
In the main menu, go to File | Open and select the esp
/esp-idf folder./examples /get-started /hello_world Click Open as Project and trust the sources.
At this point, project loading might fail.
tip
If you are working on Windows, go to Settings | Build, Execution, Deployment | Toolchain and set up the toolchain for your project. For more information, refer to Tutorial: Configure CLion on Windows.
Use the toolchain settings to specify the ESP-IDF environment script.
You will need either one or two environment files from the ESP-IDF installation. Two files are required for the case of Python virtual environment.
Go to Settings | Build, Execution, Deployment | Toolchain.
Click Add environment next to the Name field, then click From file.
Navigate to esp/esp-idf and select export.sh:
Save the toolchain settings.
The project will be reloaded automatically. In case of errors, call Tools | CMake | Reset Cache and Reload project from the main menu.
On Windows, two files are required to set up the ESP-IDF environment. To automate this, you can create a simple script that will load them both and use it as the toolchain environment file.
Create a script file in the project folder. For example, espidf_source.bat.
Customize the following lines and add them to your script:
@call C:\Espressif\python_env\idf5.0_py3.8_env\Scripts\activate.bat @call C:\Espressif\frameworks\esp-idf-v5.0-2\export.bat
Go to Settings | Build, Execution, Deployment | Toolchain.
Click Add environment next to the Name field, then click From file.
Select the newly created file.
Save the toolchain settings.
The project will be reloaded automatically. In case of errors, call Tools | CMake | Reset Cache and Reload project from the main menu.
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.
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.
tip
For video instructions, watch Developing for ESP32 With CLion on Windows: Flashing and monitoring your ESP-32 chip
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.
Select the monitor configuration and build it. If you get an error that serial port cannot be opened, then do the following:
Open the Device Manager and check the port that is used by your ESP device.
Add one more variable to the Environment variables in CMake settings:
ESPPORT
. Set the value to the port, for example,COM3
.After the project is reloaded, build the monitor configuration again. The ESP-32 chip should report back as expected.
When using the CLion legacy engine, in some cases, the size of long
can be considered 64-bit, while it 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. You shouldn't have such problems when using CLion Nova.
If you are getting the Git executable not found
error on Windows, make sure that Git is installed into a location discoverable by ESP-IDF's find_package(Git)
command.
To workaround that, add the GIT
environment variable with the full path to the git.exe binary. You can do that by modifying espfind_package(Git)
to the following:
if (DEFINED ENV{GIT})
execute_process(COMMAND $ENV{GIT} --version
OUTPUT_VARIABLE git_version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (git_version MATCHES "^git version [0-9]")
string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}")
set(GIT_FOUND True)
set(GIT_EXECUTABLE $ENV{GIT})
endif()
unset(git_version)
else()
find_package(Git)
endif ()
Thanks for your feedback!