Introduction: Build a Program for STM32 MCU Under Linux

The STM32Fx and STM32Lx are MCUs based on ARM architecture. For building programs is available gcc compiler under GNU/Linux. The gcc compiler has many settings and flags, so it is not so easy build medium size project.

In this article will be shown how to build code for STM32 MCU under Linux. It will be used these SW:

  • STM32CubeMX for generating project
  • Makefile4CubeMX to generate makefile from CubeMX project
  • Code::Block IDE for edit, build, and flash code

Whole tutorial is for GNU/Linux. This procedure was tested on Ubuntu 14.04 (trusty).

Step 1: Install Necessary Software

The first of all, install the necessary software.

Install the gcc compiler for ARM:

The gcc compiler contains tools for compiling, linking and building building code for MCU. Install these three packeges:

  • gcc-arm-none-eabi
  • binutils-arm-none-eabi
  • libnewlib-arm-none-eabi
sudo apt-get install gcc-arm-none-eabi binutils-arm-none-eabi libnewlib-arm-none-eabi

Install STM32CubeMX

STM32CubeMX is java based tool for generating C project. With this tool is possible to configure all parts of used MCU, e.g. USART, I2C, SPI, GPIO, TIMx, ....

The webpage:

The CubeMX tool has Linux support. It is easy to install:

  • Download and extract zip file
  • Run the installer
  • Follow the wizard

Depends on your ST32 hardware (STM32F0, STM32F10x...|, you need download library for your MCU. Run the STM32CubeMX and click Help->Install New Libraries. From list choose the newest library for your MCU.

Install Makefile4CubeMX

The Makefile4CubeMX is needed to generate Makefile from CubeMX project.

Download link:

How to install:

    git clone
    sudo ln -s "$PWD"/ /usr/bin/

    Install Code::Blocks IDE

    To install Code::Blocks IDE use your package manager. Under Ubuntu is available version 13.12-3:

    sudo apt-get install codeblocks

    Install stlink

    stlink is program to download builded code to MCU (flash). There exist Linux version maintained by texane, on github: The install procedure is described there. If you prefer binary files, the latest release is here:

    Step 2: Set-up the Code::Blocks IDE

    To compile and build your own program for ARM processor, it is necessary set up the C::B IDE. In "Settings- >Compiler...":

    1. In left pane select Global compiler settings
    2. In top pane select compiler: GNU GCC Compiler for ARM
    3. In center pane choose "Toolchain executables" tab and fill these values:

    Compiler instalation directory: /usr

    • C compiler: arm-none-eabi-gcc
    • C++ compiler: arm-none-eabi-g++
    • Linker for dynamic libs: arm-none-eabi-gcc
    • Linker for static libs: arm-none-eabi-ar
    • Debugger: can be empty for now
    • make program: make

    Step 3: Create STM32CubeMX Project

    Before starting a project, check presence of library for specific MCU. In menu Help->Install New Libraries, check the desired library (in my case STM32F0 ver 1.5.0). Click in "Install now" to install library on your local repository.

    Next project will be for STM32F0 MCU.

    Create new project

    There are two possibilities: Create new project

    1. by selecting MCU,
    2. by selecting STM board.

    In "New project" dialog choose Series: STM32F0, Lines: STM32F0x0 Value Line. From list choose STM32F030F4Px

    For testing purposes, check pin PA0 as GPIO_Output.

    Set project properties

    Click on setting of project: menu Project -> Settings...

    1. Type project name (e.g. InstructableF0)
    2. Change project location (if you want)
    3. Toolchain / IDE: select SW4STM32
    4. Uncheck "Generate Under Root"
    5. Click OK

    Generate C code

    Click Project->Generate code to generate empty project

    Step 4: Generate Makefile and C::B Project

    To generate Makefile and Code::Block project, the Makefile4CubeMX script will be used.

    Open directory where CubeMX project was generated and open terminal there. Type: .

    In working directory will be generated files: Makefile, InstructableF0.cbp (name of this file depend on CubeMX project name)

    Step 5: Write and Build You Program

    Open project InstructableF0.cbp and open main.c file.

    Now, we can a little bit code. Lets toggle the port PA.0 to demonstrate working code.

    int main(void){
      /* MCU Configuration-------------------------*/
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */   
      /* Configure the system clock */   
      /* Initialize all configured peripherals */   
      /* Infinite loop */   
      /* USER CODE BEGIN WHILE */   
      while (1)   {       
       HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
       HAL_Delay(500);   // 500ms   
      /* USER CODE BEGIN 3 */
    /* USER CODE END 3 */

    Build your code to verify the correctness of our code: Build->Rebuild (Ctrl+F11)

    Step 6: Connect Hardware

    Prepare your hardware:

    1. Connect stlink-v2 dongle with your board
    2. Connect the LED diode to port PA0 (in series to resistor 150 Ohm or more)


    1. In Code::Block choose "Release" in compiler toolbar.
    2. Choose "Rebuild" (Ctrl+F11). After rebuilding, the code is automatically loaded into MCU
    3. LED diode start blink.

    You can use the Makefile also without Code::Block IDE.

    Open terminal in working directory (where Makefile is placed) and type:

    Build code:


    flash code to MCU:

    make flash<br>