DEV Community

Cover image for CMake on STM32 | Episode 2: build with CLion
Pierre Gradot
Pierre Gradot

Posted on • Updated on

CMake on STM32 | Episode 2: build with CLion

In this article, you will learn how to build a CMake project for STM32 and download the software to the MCU with CLion, Jetbrains IDE for C & C++.

In the episode 1, we got a BSP along with the CMakeLists.txt:

Alt Text

Finally we built the software from the command line.

To be honest, in my every day life, I don't use the command line. I use instead CLion because it is much more confortable... Let's try it!

Why CLion?

Coding with CLion is really a pleasure and IMHO it is the best IDE for C & C++ out there. If I want to remain objective, I will just say that there are two very good reasons to talk about CLion in this series about CMake and STM32:

  1. Clion uses CMake as its default build system.
  2. It has an integrated plugin with OpenOCD so we can download / run / debug our code on STM32 microcontrollers.

Great, right?

The license is very affordable for an enterprise but 200€ per year are probably too much for a hobbyist. Anyway there is a solution:
CLion EAP version is a free version of CLion. In fact, EAP versions are beta versions of the next releases of Jetbrains IDEs. They are stable and you get some cool features ahead... for free.

Open the project in CLion

Install CLion, open it, go to File, Open and select the location of your project:

Alt Text

As soon as you click OK, CLion will parse your CMakeLists.txt and will complain:

Alt Text

As I mentioned in episode 1, we have to specify the toolchain file. Here CLion doesn't ask this to CMake so CMake uses the default native toolchain, mingw64, which is not able to compile with options that are specific to ARM GCC.

Specify the toolchain file

To specify the toolchain file, go File and then Settings. In Build, Execution, Deployment, you will find the CMake entry. This is where you control the CMake configurations and their options. Simply add the option -DCMAKE_TOOLCHAIN_FILE=arm-none-eabi-gcc.cmake (the option for the build type is added automatically based on the selection of the Build type drop-down list):

Alt Text

When you click OK, CLion reloads the CMake project with success.

Build the project

This is as simple as doing Build and Build project!

Alternatively, you can click the green hammer on the top-right of the screen:

Alt Text

You can follow the build process in the Messages view:

Alt Text

All good!

Download & run the application

CLion has an integrated plugin to download & run a software on a MCU using OpenOCD.

Let's create a run/debug configuration to program our board.

Select Run and then Edit configurations.... You will see one existing configuration:

Alt Text

This configuration (created by default when the CMake project is loaded) is bad: it will try run the executable on Windows... You can delete it with the - button.

Click the + button to create a new OpenOCD Download & Run configuration. Surprise: a board config file is requested by OpenOCD... How do we get such a file? We simply go to OpenOCD's repository on GitHub and download the appropriate file! Here is how this st_nucleo_f4.cfg looks like:

# This is for all ST NUCLEO with any STM32F4. Known boards at the moment:
# STM32F401RET6
# STM32F411RET6

source [find interface/stlink.cfg]

transport select hla_swd

source [find target/stm32f4x.cfg]

reset_config srst_only
Enter fullscreen mode Exit fullscreen mode

Select it in your run configuration:

Alt Text

Click OK to close the menu.

We can now download and run our program. Be sure the NUCLEO-F413ZH board is connected to your computer and click the green triangle next to the hammer:

Alt Text

Hell yeah!


We saw in episode 1 that our application does nothing more than an infinite idle loop... Consequence: there is no sign from the outside that our software is running.

Let's debug it so that we can check what it is actually doing! Open BSP/Src/main.c and double-click in the margin to add a breakpoint:

Alt Text

Click the green bug next to the green triangle to start the debugging session. The debugger will stop on the breakpoint:

Alt Text

This a regular debugging session: you can step over lines, step into a function, see the stack frames, add or remove breakpoint, etc. And you will end in the infinite loop at line 102.


CLion is a great IDE for C and C++ but it is especially great to work with CMake on STM32. In fact, it is great for any board / MCU that can be targeted by OpenOCD.... and there are a lot of them! If you want go get confortable, give a try! I'm sure you will like it :)

Top comments (0)