Back to Home

CMake for STM32F407: building with GCC on Windows

The article describes setting up CMake for cross-platform firmware build of STM32F407VE with ARM GCC on Windows. Detailed migration from GNU Make, HAL modules configuration and strict compiler flags. Suitable for middle/senior embedded developers.

STM32 firmware build: CMake + GCC on Win10
Advertisement 728x90

Setting Up CMake for Building STM32F407 Firmware on Windows with GCC

CMake enables cross-platform building of firmware for STM32 microcontrollers. With a single set of scripts, you can build projects on both Windows and Linux. This solves the issue of OS-specific paths and utilities in GNU Make. For single-platform development, switching isn't necessary, but when supporting multiple OSes, CMake becomes the standard among meta build systems.

The system generates native build files: GNU Make for Linux/Windows, Ninja, or IDE projects. The focus is on ARM GCC for STM32F407VE with ST's HAL and CMSIS.

Advantages of Script-Based Building

Building from the command line scales to hundreds of firmware projects. GUI IDEs limit automation as projects grow. Scripts allow you to:

Google AdInline article slot
  • Automate CI/CD pipelines
  • Build all variants overnight
  • Integrate into repositories with a single command

For 150+ firmware projects, a batch file is enough: run it, and within minutes, .hex files are ready.

Task Definition: A C project for STM32F407VE. ARM GCC cross-compiler. CMake generates GNU Make. STM32 HAL + CMSIS. Target: Windows 10.

Pass strict flags to the compiler:

Google AdInline article slot
-MMD -MP -O0 -std=c11 -Wall -Werror -mcpu=cortex-m4 -march=armv7e-m -Werror=address -Werror=address-of-packed-member -Werror=array-bounds=1 -Werror=bool-compare -Werror=bool-operation -Werror=char-subscripts -Werror=clobbered -Werror=div-by-zero -Werror=duplicate-decl-specifier -Werror=empty-body -Werror=enum-compare -Werror=float-equal -Werror=ignored-qualifiers -Werror=implicit -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=init-self -Werror=int-in-bool-context -Werror=int-to-pointer-cast -Werror=logical-not-parentheses -Werror=logical-op -Werror=maybe-uninitialized -Werror=memset-elt-size -Werror=misleading-indentation -Werror=missing-braces -Werror=multistatement-macros -Werror=old-style-declaration -Werror=overflow -Werror=pointer-arith -Werror=pointer-sign -Werror=return-local-addr -Werror=return-type -Werror=shadow -Werror=shift-count-overflow -Werror=sign-compare -Werror=sizeof-pointer-div -Werror=strict-aliasing -Werror=switch -Werror=tautological-compare -Werror=type-limits -Werror=uninitialized -Werror=unused-variable -g3 -Wextra -Wno-conversion -Wno-cpp -Wno-discarded-qualifiers -Wno-implicit -Wno-int-conversion -Wno-nonnull-compare -Wno-redundant-decls -Wno-restrict -Wno-sign-compare -Wno-stringop-truncation -Wno-switch-bool -Wno-unused-parameter -fallthrough -fdata-sections -ffreestanding -ffunction-sections -finline-small -fmax-errors=70 -fno-common -fno-move-loop-invariants -fno-printf-return-value -fomit-frame-pointer -fshort-enums -fsigned-char -fstack-usage -fzero-initialized-in-bss -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mthumb

To the linker:

-specs=nosys.specs -Xlinker --gc-sections -Xlinker --nmagic -Wl,--gc-sections -Xlinker --print-memory-usage -t -Wl,--cref -Wl,--gc-sections --verbose -mcpu=cortex-m4 -march=armv7e-m -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -lm

STM32CubeMX doesn't generate CMake directly—only GNU Make. The solution: manual migration.

Theory: From Make to CMake

Environment variables store paths, compiler flags, and settings. Visible to all utilities.

Google AdInline article slot

Build system automates compilation, linking, and dependencies. Examples: GNU Make, Ninja, IAR.

Build system generator (CMake, Meson) creates files for native build systems.

CMake + GNU Make scheme:

  • CMake reads CMakeLists.txt
  • Generates Makefile
  • GNU Make builds artifacts

Check CMake: where cmake in Windows.

Migrating Make → CMake

Mechanical syntax replacement. CMake is more verbose but more powerful.

| GNU Make | CMake | Explanation |

|----------|--------|-----------|

| VAR += value | string(APPEND VAR " value") | Appending to a variable |

| CRC=Y | set(CRC Y) | Definition |

| $(VAR) | ${VAR} | Insertion |

| ifeq($(IAR),Y) | if(IAR STREQUAL Y) | Condition |

| SOURCES_C += src/main.c | target_sources(app PRIVATE src/main.c) | Source files |

| OPT += -Ipath | target_include_directories(app PUBLIC path) | Paths |

Transformation examples:

  • LINKER_FLAGS += -u _printf_floattarget_link_options(app PRIVATE -u _printf_float)
  • include file.mkinclude(${file}.cmake)
  • SOURCES += file.cstring(APPEND SOURCES " ${file}.c")
  • ifneq($(FLAG),Y)if(NOT (FLAG STREQUAL Y))

CMakeLists.txt (root):

cmake_minimum_required(VERSION 3.16)
project(jz_f407vet6_mbr_gcc_cmake)
set(PROJECT_NAME jz_f407vet6_mbr_gcc_cmake)
enable_language(C ASM)

set(CURRENT_CMAKELISTS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(PROJECT_LOC ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${PROJECT_LOC})

set(BUILD_DIR ${PROJECT_LOC}/build)
set(TARGET ${PROJECT_NAME})
set(EXECUTABLE ${TARGET})

get_filename_component(WORKSPACE_LOC "${PROJECT_LOC}/../.." ABSOLUTE)
include_directories(${WORKSPACE_LOC})

include(${PROJECT_LOC}/config.cmake)
include(${WORKSPACE_LOC}/cmake_scripts/code_base.cmake)
include(${WORKSPACE_LOC}/cmake_scripts/rules.cmake)

Component Configuration

In config.cmake, activate modules:

set(CONTROL Y)
set(DWT Y)
set(ARM_GCC Y)
set(CORTEX_M4 Y)
set(FLASH Y)
set(FPU Y)
set(GPIO Y)
set(INTERRUPT Y)
set(JZ_F407VET6 Y)
set(LED_MONO Y)
set(MBR Y)
set(MCAL_STM32 Y)
set(MICROCONTROLLER Y)
set(NVIC Y)
set(RCC Y)
set(SCHEDULER Y)
set(STM32F407VE Y)
set(STM32F4X_HAL_DRIVER Y)
set(SUPER_CYCLE Y)
set(SYSTEM Y)
set(SYSTICK Y)
set(SYS_INIT Y)
set(TIME Y)

In code_base.cmake, add components conditionally:

if(GPIO STREQUAL Y)
  target_sources(app PRIVATE gpio/src/gpio.c)
  target_include_directories(app PUBLIC gpio/inc)
  add_compile_definitions(GPIO_ENABLED)
endif()

Key Takeaways

  • CMake provides cross-platform compatibility without rewriting scripts
  • Make→CMake migration is a template-based replacement, automatable with AI
  • Strict -Werror flags catch errors at compile time
  • config.cmake manages HAL module selection
  • Script-based building scales to 100+ firmware projects

— Editorial Team

Advertisement 728x90

Read Next