ESP32 Development with CLion

Since I wasn’t used to VSCode’s half-baked C++ IDE features, I turned to CLion. But CLion in turn faces the problem of having fewer plugins than VSCode, so I ran into issues when developing for the ESP32. Finally, let me stress: if you primarily use VSCode for development, just use the official plugin provided by ESP32 — it’s simple and convenient, no command line required.

Steps

Actually, there is no need to bother with environment variables or cross-compilation toolchains. I’ve tried them, and they’re useless — they only add unnecessary trouble.

Just open the ESP32 project directly with the default toolchain, as long as CMake is provided. Compile once with idf.py build, then reload the CMake project in CLion, and CLion will seemingly recognize the build artifacts on its own and configure automatically.

Then you can happily edit code with intelligent suggestions.

If you occasionally run into code recognition issues, just repeat the steps above.

Windows is even simpler: after installing ESP-IDF, two command-line shortcuts are placed on the desktop, ESP-IDF <version> CMD and ESP-IDF <version> PowerShell. Pick either one and type clion, and the CLion that opens will automatically use ESP-IDF’s variables.

Issues

The downside is that there is no official support after all. Based on my testing, only the build and flash commands execute successfully; other commands like monitor are either ineffective or malfunction.

It’s better to type commands by hand in the command line and use CLion’s code-editing assistance features for development. If you find that troublesome, you can also manually add a Run/Debug Configuration, except that it cannot be used as a template across projects — it has to be reconfigured for every project.

If that’s too much trouble, you can also set up aliases for the commands:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
alias get-idf=". $HOME/esp/esp-idf/export.sh"
alias idf-set-target="idf.py set-target"
alias idf-menuconfig="idf.py menuconfig"
alias idf-build="idf.py build"
alias idf-flash="idf.py flash"
alias idf-monitor="idf.py monitor"
alias idf-reconfigure="idf.py reconfigure"

alias idf-clean="idf.py clean"
alias idf-fullclean="idf.py fullclean"
alias idf-erase-flash="idf.py erase-flash"

In addition, on Linux systems you may also run into the issue of no permission for the serial port. Here’s how to solve it:

Check the permissions of the serial port ttyUSB0:

1
ls -l /dev/ttyUSB0

You can see that the owning user group is dialout, and only the root user has the permission to operate it.

Check the current user:

1
whoami

Then add the current user to the dialout group so that they have permission to operate the serial port ttyUSB0:

1
sudo usermod -aG dialout jove

Done, just reboot the system.

References