CMake + STM32 Cross-Compile: A Practical Starter
When I moved STM32 projects to CMake, the biggest win was repeatable builds.
No IDE magic, just files and commands.
This is a practical starter for STM32 cross-compilation.
1) What you need
Install ARM GNU Toolchain (arm-none-eabi-gcc) and make sure these are in PATH:
arm-none-eabi-gccarm-none-eabi-g++arm-none-eabi-objcopyarm-none-eabi-size
Check:
1 | arm-none-eabi-gcc --version |
2) Example project layout
1 | stm32-blink/ |
3) Toolchain file
cmake/toolchain-stm32f103.cmake:
1 | set(CMAKE_SYSTEM_NAME Generic) |
4) CMakeLists.txt
1 | cmake_minimum_required(VERSION 3.20) |
5) Minimal main.c
1 | int main(void) { |
6) Build command
1 | cmake -S . -B build \ |
After build, you should get:
stm32_blink(ELF)stm32_blink.hexstm32_blink.binstm32_blink.map
7) Flash to board (example)
If you use ST-LINK:
1 | st-flash write build/stm32_blink.bin 0x8000000 |
This setup is small, predictable, and easy to version-control.
Once this baseline works, you can add HAL, FreeRTOS, and board-specific modules step by step.
- Title: CMake + STM32 Cross-Compile: A Practical Starter
- Author: Jasper
- Created at : 2026-03-06 17:35:40
- Updated at : 2026-03-06 17:35:40
- Link: https://jasperzpzhang.github.io/stm32-cmake-cross-compile-guide/
- License: This work is licensed under CC BY-NC-SA 4.0.