{"id":50128763,"url":"https://github.com/jasonyang-ee/STM32-CMAKE-TEMPLATE","last_synced_at":"2026-06-26T13:01:19.327Z","repository":{"id":93734138,"uuid":"534502034","full_name":"jasonyang-ee/STM32-CMAKE-TEMPLATE","owner":"jasonyang-ee","description":"STM32 Template Project with Using CMake.","archived":false,"fork":false,"pushed_at":"2025-03-14T23:14:38.000Z","size":2539,"stargazers_count":53,"open_issues_count":0,"forks_count":9,"subscribers_count":3,"default_branch":"main","last_synced_at":"2026-02-07T12:31:14.771Z","etag":null,"topics":["arm","cmake","cortex-m","gcc","stm32","toolchain"],"latest_commit_sha":null,"homepage":"http://doc.jasony.org/STM32-CMAKE-TEMPLATE/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasonyang-ee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"jasonyang-ee"}},"created_at":"2022-09-09T04:50:57.000Z","updated_at":"2026-01-24T19:54:54.000Z","dependencies_parsed_at":"2023-12-22T19:26:08.608Z","dependency_job_id":"b1cc8f97-1ff5-405a-a879-c879a2b01162","html_url":"https://github.com/jasonyang-ee/STM32-CMAKE-TEMPLATE","commit_stats":null,"previous_names":[],"tags_count":3,"template":true,"template_full_name":null,"purl":"pkg:github/jasonyang-ee/STM32-CMAKE-TEMPLATE","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonyang-ee%2FSTM32-CMAKE-TEMPLATE","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonyang-ee%2FSTM32-CMAKE-TEMPLATE/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonyang-ee%2FSTM32-CMAKE-TEMPLATE/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonyang-ee%2FSTM32-CMAKE-TEMPLATE/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasonyang-ee","download_url":"https://codeload.github.com/jasonyang-ee/STM32-CMAKE-TEMPLATE/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonyang-ee%2FSTM32-CMAKE-TEMPLATE/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34817641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-26T02:00:06.560Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm","cmake","cortex-m","gcc","stm32","toolchain"],"created_at":"2026-05-23T21:00:29.530Z","updated_at":"2026-06-26T13:01:19.302Z","avatar_url":"https://github.com/jasonyang-ee.png","language":"C","funding_links":["https://github.com/sponsors/jasonyang-ee"],"categories":["C"],"sub_categories":[],"readme":"[![Build Test](https://github.com/jasonyang-ee/STM32-CMAKE-TEMPLATE/actions/workflows/build.yml/badge.svg)](https://github.com/jasonyang-ee/STM32-CMAKE-TEMPLATE/actions/workflows/build.yml)\n[![Deploy Docs](https://github.com/jasonyang-ee/STM32-CMAKE-TEMPLATE/actions/workflows/mdbook.yml/badge.svg)](https://github.com/jasonyang-ee/STM32-CMAKE-TEMPLATE/actions/workflows/mdbook.yml)\n\n\n# STM32 CMake Template\n\nA CMake template repo to allow quick porting to start a new STM32 project.\n\nThis instruction will be focusing on Windows environment setup with using VS Code.\n\nProject using STM32L432KC as example. Test hardware is NUCLEO-L432KC.\n\n## Documentation\n\n### Visit [Documentation](https://doc.jasony.org/STM32-CMAKE-TEMPLATE/) for more information.\n\n\u0026nbsp;\n\n\u0026nbsp;\n\n\u0026nbsp;\n\n# Simplified Instruction\n\n## Toolchain\n\n- [ARM GNU](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads)\n- [CMake](https://cmake.org/download/)\n- [Ninja](https://github.com/ninja-build/ninja/releases)\n- ST Link GDB Server (Copy from CubeIDE Installation).\n- STM32_Programmer_CLI (Copy from CubeIDE Installation)\n\n\n\u0026nbsp;\n\n\n## VS Code Extensions\n\n- CMake\n- CMake Tools\n- Cortex-Debug\n- Memory View\n- RTOS Views\n\n\n\u0026nbsp;\n\n\n\n## CMakeList.txt file\n\nThis is the main CMake setup file.\n\n- Make new file in project root: `CMakeList.txt`\n\n- Modify `project name`, `linker file`, and `MCU sepecific setting`.\n\n- If you are using this project folder structure, you may run the bash script `.\\getIncludeList.sh` and `.\\getSourceList.sh` to auto scan `/Application` folder for generating CMake source list.\n  \n- Otherwise, you will have to modify `/camke/IncludeList.cmake` and `/cmake/IncludeList.cmake`.\n\n\u003e CMakeList.txt\n\n```makefile\n# Define needed CMake verion\ncmake_minimum_required(VERSION 3.22)\n\n\n# Setup cmake module path and compiler settings\nlist(APPEND CMAKE_MODULE_PATH \"${CMAKE_CURRENT_LIST_DIR}/cmake\")\n# Print current build type to console\nmessage(\"Build type: \"              ${CMAKE_BUILD_TYPE})\n# Setup C and C++ version\nset(CMAKE_C_STANDARD                11)\nset(CMAKE_C_STANDARD_REQUIRED       ON)\nset(CMAKE_C_EXTENSIONS              ON)\nset(CMAKE_CXX_STANDARD              17)\nset(CMAKE_CXX_STANDARD_REQUIRED     ON)\nset(CMAKE_CXX_EXTENSIONS            ON)\nset(CMAKE_EXPORT_COMPILE_COMMANDS   ON)\n# Define current path for shorter reference below\nset(PROJ_PATH                       ${CMAKE_CURRENT_SOURCE_DIR})\n# Define .cmake module for toolchain compile flags that does holds true for all ARM projects\n# This path is defined in the list() function above\nset(CMAKE_TOOLCHAIN_FILE            gcc-arm-none-eabi)\n\n\n# Project Name    --- MUST EDIT ---\nproject(L432KC-Template)\n# Part of project name but made seperate for ease of editing project name\nenable_language(C CXX ASM)\n# Linker File     --- MUST EDIT ---\nset(linker_script_SRC               ${PROJ_PATH}/Core/STM32L432KCUX_FLASH.ld)\n# The use project name for binary file name\nset(EXECUTABLE                      ${CMAKE_PROJECT_NAME})\n\n\n# MCU Sepecific Setting    --- MUST EDIT ---\n# Make multiple for various STM32 core\n# This path is defined in the list() function above\ninclude(STM32L432xx_HAL_PARA)\n\n# .cmake module generated by using .\\getIncludeList.sh and .\\getSourceList.sh\n# Those two file contains all the project source file list and include list\n# This path is defined in the list() function above\ninclude(SourceList)\ninclude(IncludeList)\n\n\n# Executable files\nadd_executable(${EXECUTABLE} ${source_list})\n# Include paths\ntarget_include_directories(${EXECUTABLE} PRIVATE ${include_list})\n# Project symbols\ntarget_compile_definitions(${EXECUTABLE} PRIVATE ${compiler_define})\n# Compiler options\ntarget_compile_options(${EXECUTABLE} PRIVATE\n\t${CPU_PARAMETERS}\n\t-Wall\n\t-Wpedantic\n\t-Wno-unused-parameter\n)\n# Linker options\ntarget_link_options(${EXECUTABLE} PRIVATE\n\t-T${linker_script_SRC}\n\t${CPU_PARAMETERS}\n\t-Wl,-Map=${CMAKE_PROJECT_NAME}.map\n\t--specs=nosys.specs\n\t#-u _printf_float                # STDIO float formatting support\n\t-Wl,--start-group\n\t-lc\n\t-lm\n\t-lstdc++\n\t-lsupc++\n\t-Wl,--end-group\n\t-Wl,--print-memory-usage\n)\n# Execute post-build to print size\nadd_custom_command(TARGET ${EXECUTABLE} POST_BUILD\n\tCOMMAND ${CMAKE_SIZE} $\u003cTARGET_FILE:${EXECUTABLE}\u003e\n)\n# Convert output to hex and binary\nadd_custom_command(TARGET ${EXECUTABLE} POST_BUILD\n\tCOMMAND ${CMAKE_OBJCOPY} -O ihex $\u003cTARGET_FILE:${EXECUTABLE}\u003e ${EXECUTABLE}.hex\n)\n# Convert to bin file -\u003e add conditional check?\nadd_custom_command(TARGET ${EXECUTABLE} POST_BUILD\n\tCOMMAND ${CMAKE_OBJCOPY} -O binary $\u003cTARGET_FILE:${EXECUTABLE}\u003e ${EXECUTABLE}.bin\n)\n```\n\n\n\n\u0026nbsp;\n\n\n\n\n## Toolchain file\n\nCMake needs to be aware about toolchain we would like to use to finally compile the project with. This file will be universal across projects.\n\n- Make new folder in project root: `cmake`\n- Make new file in folder /cmake: `./cmake/gcc-arm-none-eabi.cmake`\n\n\u003e gcc-arm-none-eabi.cmake\n\n```makefile\nset(CMAKE_SYSTEM_NAME               Generic)\nset(CMAKE_SYSTEM_PROCESSOR          arm)\n\n# Some default GCC settings\n# arm-none-eabi- must be part of path environment\nset(TOOLCHAIN_PREFIX                arm-none-eabi-)\nset(FLAGS                           \"-fdata-sections -ffunction-sections --specs=nano.specs -Wl,--gc-sections\")\nset(CPP_FLAGS                       \"-fno-rtti -fno-exceptions -fno-threadsafe-statics\")\n\n# Define compiler settings\nset(CMAKE_C_COMPILER                ${TOOLCHAIN_PREFIX}gcc ${FLAGS})\nset(CMAKE_ASM_COMPILER              ${CMAKE_C_COMPILER})\nset(CMAKE_CXX_COMPILER              ${TOOLCHAIN_PREFIX}g++ ${FLAGS} ${CPP_FLAGS})\nset(CMAKE_OBJCOPY                   ${TOOLCHAIN_PREFIX}objcopy)\nset(CMAKE_SIZE                      ${TOOLCHAIN_PREFIX}size)\n\nset(CMAKE_EXECUTABLE_SUFFIX_ASM     \".elf\")\nset(CMAKE_EXECUTABLE_SUFFIX_C       \".elf\")\nset(CMAKE_EXECUTABLE_SUFFIX_CXX     \".elf\")\n\nset(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)\n```\n\n\n\n\n\u0026nbsp;\n\n\n\n\n## MCU sepecific file\n\nEach MCU has their own ARM compiler flags. Those are defined in a individual module for portability.\n\n\u003e STM32L432xx_HAL_PARA.cmake\n\n```makefile\nset(CPU_PARAMETERS ${CPU_PARAMETERS}\n    -mthumb\n    -mcpu=cortex-m4\n    -mfpu=fpv4-sp-d16\n    -mfloat-abi=hard\n)\n\nset(compiler_define ${compiler_define}\n    \"USE_HAL_DRIVER\"\n    \"STM32L432xx\"\n)\n```\n\n\u003e **General rule for settings would be as per table below:**\n\n| STM32 Family | -mcpu           | -mfpu         | -mfloat-abi |\n| ------------ | --------------- | ------------- | ----------- |\n| STM32F0      | `cortex-m0`     | `Not used`    | `soft`      |\n| STM32F1      | `cortex-m3`     | `Not used`    | `soft`      |\n| STM32F2      | `cortex-m3`     | `Not used`    | `soft`      |\n| STM32F3      | `cortex-m4`     | `fpv4-sp-d16` | `hard`      |\n| STM32F4      | `cortex-m4`     | `fpv4-sp-d16` | `hard`      |\n| STM32F7 SP   | `cortex-m7`     | `fpv5-sp-d16` | `hard`      |\n| STM32F7 DP   | `cortex-m7`     | `fpv5-d16`    | `hard`      |\n| STM32G0      | `cortex-m0plus` | `Not used`    | `soft`      |\n| STM32C0      | `cortex-m0plus` | `Not used`    | `soft`      |\n| STM32G4      | `cortex-m4`     | `fpv4-sp-d16` | `hard`      |\n| STM32H7      | `cortex-m7`     | `fpv5-d16`    | `hard`      |\n| STM32L0      | `cortex-m0plus` | `Not used`    | `soft`      |\n| STM32L1      | `cortex-m3`     | `Not used`    | `soft`      |\n| STM32L4      | `cortex-m4`     | `fpv4-sp-d16` | `hard`      |\n| STM32L5      | `cortex-m33`    | `fpv5-sp-d16` | `hard`      |\n| STM32U5      | `cortex-m33`    | `fpv5-sp-d16` | `hard`      |\n| STM32WB      | `cortex-m4`     | `fpv4-sp-d16` | `hard`      |\n| STM32WL CM4  | `cortex-m4`     | `Not used`    | `soft`      |\n| STM32WL CM0  | `cortex-m0plus` | `Not used`    | `soft`      |\n\n\n\n\n\u0026nbsp;\n\n\n\n\n\n## Source List and Include List file\n\nProject source and include list are required for CMake to build the project in `/cmake` folder.\n\nThe format of the list must be full path.\n\n\n\u0026nbsp;\n\n\n\n## Auto Scan Source and Include List\n\nAuto scan bash script has been made for STM32CubeMX generated files structure\n\n- In terminal `` Ctrl + ` ``, run `.\\getIncludeList.sh` and `.\\getSourceList.sh`\n\n- A list of scanned source and header will be saved in `/cmake` folder.\n\n\u003e You may modify bash file to expend the auto file searching for more folders.\n\n\u003e The bash simply scan `.c` `.cpp` `.s` file for source. And, it scan `/Inc` `/Include` for include path.\n\n\n\n\n\u0026nbsp;\n\n\n\n\n\n## CMakePresets.json file\n\n`CMakePresets.json` provides definition for user configuration. Having this file allows developer to quickly change between debug and release mode.\n\n- Create file `CMakePresets.json` in Project Root\n\n\u003e CMakePresets.json\n\n```json\n{\n  \"version\": 3,\n  \"configurePresets\": [\n    {\n      \"name\": \"default\",\n      \"hidden\": true,\n      \"generator\": \"Ninja\",\n      \"binaryDir\": \"${sourceDir}/build/${presetName}\",\n      \"toolchainFile\": \"${sourceDir}/cmake/gcc-arm-none-eabi.cmake\",\n      \"cacheVariables\": {\n      \"CMAKE_EXPORT_COMPILE_COMMANDS\": \"ON\"\n      }\n    },\n    {\n      \"name\": \"Debug\",\n      \"inherits\": \"default\",\n      \"cacheVariables\": {\n      \"CMAKE_BUILD_TYPE\": \"Debug\"\n      }\n    },\n    {\n      \"name\": \"RelWithDebInfo\",\n      \"inherits\": \"default\",\n      \"cacheVariables\": {\n      \"CMAKE_BUILD_TYPE\": \"RelWithDebInfo\"\n      }\n    },\n    {\n      \"name\": \"Release\",\n      \"inherits\": \"default\",\n      \"cacheVariables\": {\n      \"CMAKE_BUILD_TYPE\": \"Release\"\n      }\n    },\n    {\n      \"name\": \"MinSizeRel\",\n      \"inherits\": \"default\",\n      \"cacheVariables\": {\n      \"CMAKE_BUILD_TYPE\": \"MinSizeRel\"\n      }\n    }\n  ]\n}\n```\n\n\n\u0026nbsp;\n\n\n\n\n\n## Debug Project \n\nThis is using VS Code `Tasks` feature and Extention `cortex-debug`\n\n- Create `.vscode/launch.json`\n\n- Open debug tab. And our named debug preset `ST-Link` should be available to run `Green Icon` or `F5`.\n\n\u003e launch.json\n\n```json\n{\n  \"version\": \"0.2.0\",\n  \"configurations\": [\n    {\n      \"name\": \"ST-Link\",\n      \"cwd\": \"${workspaceFolder}\",\n      \"executable\": \"${command:cmake.launchTargetPath}\",\n      \"request\": \"launch\",\n      \"type\": \"cortex-debug\",\n      \"servertype\": \"stlink\",\n      \"interface\": \"swd\",\n      \"showDevDebugOutput\": \"both\",\n      \"v1\": false,                            // ST-Link version\n      \"device\": \"STM32L432KC\",                // MCU used [optional]\n      \"serialNumber\": \"\",                     // Set ST-Link ID if you use multiple at the same time [optional]\n      \"runToEntryPoint\": \"main\",              // Run to main and stop there [optional]\n      \"svdFile\": \"STM32_svd/STM32L4x2.svd\"    // SVD file to see registers [optional]\n\n      // \"servertype\": \"stlink\", will try to run command \"STM32_Programmer_CLI\", \"ST-LINK_gdbserver\", and  which must exist in your system PATH.\n\n      // If using SWO to see serial wire view, you will have to setup \"servertype\": \"OpenOCD\". Please refer to the extension github page to learn details.\n    }\n  ]\n}\n```\n\n\n\n\u0026nbsp;\n\n\n\n\n\n## Flash to Target\n\nWe are using VS Code Task `Ctrl + Shift + P` -\u003e Enter `Tasks: run task`. This will allow auto excution of custom terminal commands.\n\nSetting keyboard short cut `Ctrl + T` for this is going to help you very much.\n\nThe configuration can be defined by creating file `.vscode/tasks.json`\n\n\u003e tasks.json\n\n```json\n{\n  \"version\": \"2.0.0\",\n  \"tasks\": [\n    {\n      \"type\": \"shell\",\n      \"label\": \"Windows: Flash Firmware\",\n      \"command\": \"STM32_Programmer_CLI\",\n      \"args\": [\n        \"--connect\",\n        \"port=swd\",\n        \"--download\",\n        \"${command:cmake.launchTargetPath}\",\n        \"-rst\",\n        \"-run\"\n      ],\n      \"options\": {\n        \"cwd\": \"${workspaceFolder}\"\n      },\n      \"problemMatcher\": []\n    }\n  ]\n}\n```\n\n\n\n\u0026nbsp;\n\n\n\n## Docker Container for STM32 CMake \u0026 Ninja Compiling\n\n### Dockerfile Details: https://github.com/jasonyang-ee/STM32-Dockerfile.git\n\n-+- TL;DR -+-\n\nThis docker image auto clone an online git repo and compile the CMake \u0026 Ninja supported STM32 project locally on your computer with mounted volume.\n```bash\ndocker run -v \"{Local_Full_Path}\":\"/home\" jasonyangee/stm32-builder:ubuntu-latest -r {Git_Repo_URL}\n```\n\n![Run](docs_src/page/img/run_time.gif)\n\n\n### Docker Image\n\nPublic Registry:\n\u003e ghcr.io/jasonyang-ee/stm32-builder:ubuntu-latest\n\n\u003e ghcr.io/jasonyang-ee/stm32-builder:alpine-latest\n\n\u003e ghcr.io/jasonyang-ee/stm32-builder:arch-latest\n\n\u003e jasonyangee/stm32-builder:ubuntu-latest\n\n\u003e jasonyangee/stm32-builder:alpine-latest\n\n\u003e jasonyangee/stm32-builder:arch-latest\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonyang-ee%2FSTM32-CMAKE-TEMPLATE","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasonyang-ee%2FSTM32-CMAKE-TEMPLATE","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonyang-ee%2FSTM32-CMAKE-TEMPLATE/lists"}