Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

stm32-cube-cmake-vscode

> 开发工具
Open source

STM32, VSCode and CMake detailed tutorial

717 stars0 likes2 views
WebsiteGitHub

About

STM32, VSCode and CMake detailed tutorial

STM32 development environment with Visual Studio Code and CMake

This tutorial explains steps to effectively develop and debug STM32 application in Visual Studio Code using CMake build generator, Ninja build tool and GCC compiler.

Things you will learn

  • How to install and setup all tools
  • How to create new STM32 project with STM32CubeMX or STM32CubeIDE tools
  • How to install and setup recommended extensions for Visual Studio Code for easier development
  • How to setup CMake lists and CMake presets
  • How to generate build system for compiler
  • How to compile the project with GCC
  • How to flash and debug application to the STM32 target

This tutorial is using Windows operating system. Similar procedure will apply for Linux and MAC operating system.

Tools installation

August 2024 update

The tools installation tutorial will help you to understand the necessary tools required to work with STM32 and is generally good for beginners to get up to speed and correctly understand necessary requirements.

Since some time, ST has a new STM32CubeCLT software tool, that includes the build tools necessary for vscode, includes Ninja build system and CMake build generator. STM32CubeCLT (Command Line Tools) is a simple and easy way to quickly get up to speed for vscode development. It will also setup your build environment variables (Path in case of Windows) automatically, allowing you to invoke ninja, cmake or other commands directly from command line tool.

By installing the STM32CubeCLT, you won't get STM32CubeIDE graphical tool, nor you will get any MCU configuration tool capability, rather just command line tools to invoke build and debug, normally from within vscode. You will still need to separately install vscode and necessary extensions.

STM32CubeIDE

First step is to install STM32CubeIDE, that will be used to easily start new STM32 project and it comes with integrated STM32CubeMX tool - allowing us graphical configuration.

STM32CubeIDE also provides necessary tools needed later for VSCode development

  • ARM none eabi GCC compiler
  • ST-LINK GDBServer for debugging
  • STM32CubeProgrammer tool for code downloading and respective ST-Link drivers
  • Folder with STM32 SVD files
  • Drivers for ST-Link

Environmental path setup

3 paths should be added to environmental settings from STM32CubeIDE installation, one path for each of above-mentioned tools. In case of my computer, using STM32CubeIDE 1.8 (updated through eclipse, hence my actual installation path is still showing version 1.0.2) paths are defined as:

  • GCC compiler: c:\ST\STM32CubeIDE_1.0.2\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\bin\
  • ST-Link GDB server: c:\ST\STM32CubeIDE_1.0.2\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.stlink-gdb-server.win32_2.0.100.202109301221\tools\bin\
  • STM32Cube Programmer CLI: c:\ST\STM32CubeIDE_1.0.2\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.win32_2.0.100.202110141430\tools\bin\

Your paths may differ at version numbers

Verify correct path setup, run:

arm-none-eabi-gcc --version
STM32_Programmer_CLI --version
ST-LINK_gdbserver --version

That should produce output similar to the picture below

CMake

This step is not necessary if you have installed the build tools with STM32CubeCLT

Download and install CMake.

Installation wizard will ask you to add CMake to environmental paths. Select the option or add bin folder of CMake installation folder to environmental path.

Ninja

This step is not necessary if you have installed the build tools with STM32CubeCLT

Download Ninja build system from Github releases page. It comes as portable executable, without need to install anything. However it must be visible at environment level, like all previous tools.

Verify CMake and Ninja installation, run:

cmake --version
ninja --version

Output shall be something similar to

Visual Studio Code

Download and install VSCode. Once installed and opened, window will look similar to the one below.

Visual Studio Code extensions

Visual Studio Code is lightweight text editor with capability to enlarge it using extensions.

List of useful extensions for STM32 development using CMake:

  • ms-vscode.cpptools: Syntax highlighting and other core features for C/C++ development
  • ms-vscode.cmake-tools: CMake core tools, build system generator tool
  • twxs.cmake: CMake color highlighting
  • marus25.cortex-debug: Cortex-M debugging extension, mandatory for STM32 debug from VSCode
  • dan-c-underwood.arm: ARM Assembly syntax highlighter
  • zixuanwang.linkerscript: GCC Linker script syntax highlighter

You can install them by copying below commands in VSCode's internal terminal window.

code --install-extension ms-vscode.cpptools
code --install-extension ms-vscode.cmake-tools
code --install-extension twxs.cmake
code --install-extension marus25.cortex-debug
code --install-extension dan-c-underwood.arm
code --install-extension zixuanwang.linkerscript

Go to Terminal -> New Terminal to open new terminal window

Alternative way is to use Extension search GUI and manually install from there.

Tools installed - checkpoint

At this point, all the tools are properly installed - you are on the right track towards success.

New project creation

Fundamental requirement to move forward is to have a working project that will be converted to CMake and developed in VSCode. For this purpose, I will guide you through simple new project creation using STM32CubeMX or STM32CubeIDE software tools.

You can skip this part, if you already have your project to work on.

I used STM32CubeIDE tool and STM32H735G-DK board for this demo.

Open STM32CubeIDE and start new project

Select STM32 MCU - I selected STM32H735IG which is used on STM32H735G-DK board

Select project name and path, then create project and wait for Pinout view to open

Our task is to have a simple project that will toggle leds. LEDs are connected to PC2 and PC3 respectively, active LOW. Pins can be configured in output push-pull or open-drain mode

Set pins as outputs with optional labels as LED1 and LED2 respectively

If you are using STM32CubeMX, go to Project manager, set project name and be sure STM32CubeIDE is selected as Toolchain.

Go to advanced settings and select LL as drivers for generated code

We are using LL drivers for the sake of simplicity in this tutorial

Re-generate the project by pressing red button or by saving the project with CTRL + S shortcut

Project is now (re)generated. Yellow highlighted files are sources to build. Blue is linker script.

That's it for the first run, we are ready to compile. Hit CTRL + B or click on hammer icon to start. STM32CubeIDE will compile the project, you should see similar as on picture below. It is now ready for flashing the MCU's flash and start debugging.

This is end of first part, where we successfully created our project. At this point we consider project being ready to be transferred to CMake-based build system.

You can continue your development with STM32CubeIDE in the future, add new sources, modify code, compile, flash the binary and debug directly the microcontroller. This is preferred STM32 development studio, developed and maintained by STMicroelectronics.

CMake configuration

It is expected that project to develop in VSCode has been created. We will move forward for GCC compiler, but others could be used too.

With release of Visual Studio Code, many developers use the tool for many programming languages and fortunately can also develop STM32 applications with single tool. If you are one of developers liking VSCode, most elegant way to move forward is to transfer STM32CubeIDE-based project to CMake, develop code in VSCode and compile with Ninja build system using GCC compiler. It is fast and lightweight.

Development in VSCode is for intermediate or experienced users. I suggest to all STM32 beginners to stay with STM32CubeIDE development toolchain. It will be very easy to move forward and come to VSCode topic later.

Prepare CMakeLists.txt file

Every CMake-based application requires CMakeLists.txt file in the root directory, that describes the project and provides input information for build system generation.

Root CMakeLists.txt file is sometimes called top-level CMake file

Essential things described in CMakeLists.txt file:

  • Toolchain information, such as GCC configuration with build flags
  • Project name
  • Source files to build with compiler, C, C++ or Assembly files
  • List of include paths for compiler to find functions, defines, ... (-I)
  • Linker script path
  • Compilation defines, or sometimes called preprocessor defines (-D)
  • Cortex-Mxx and floating point settings for instruction set generation

Open VSCode in project root folder

Visual Studio Code has been installed and will be used as further file editor.

Find your generated project path and open folder with VSCode:

  • Option 1: Go to the folder with explorer, then right click and select Open in Code.
  • Option 2: Alternatively, open VScode as new empty solution and add folder to it manually. Use File -> Open Folder... to open folder
  • Option 3: Go to folder with cmd or powershell tool and run code .

Final result should look similar to the one below

Toolchain information

CMake needs to be aware about Toolchain we would like to use to finally compile the project with. As same toolchain is usually reused among different projects, it is advised to create this part in separate file for easier reuse. These are generic compiler settings and not directly linked to projects itself.

A simple .cmake file can be used and later reused among your various projects. I am using name cmake/gcc-arm-none-eabi.cmake for this tutorial and below is its example:

…

Create a file in the cmake/ folder of root project directory.

If CMake highlighter plugin is installed, VSCode will nicely highlight CMake commands for you

Toolchain setup is complete. You can freely close the file and move to next step.

Create main CMakeLists.txt file

We need to create main CMakeLists.txt, also called root CMake file.

Make sure you really name it CMakeLists.txt with correct upper and lowercase characters.

I prepared simple template file for you, that can be reused for all of your projects in the future. You will just need to change things like project name, source files, include paths, etc.

…

Source files are the same as in STM32CubeIDE project. You can check previous image with highlighted sources in yellow color.

Symbols and include paths can be found in STM32CubeIDE under project settings. 2 pictures below are showing how it is in the case of demo project.

Cortex-Mxx setup needs a special attention, especially with floating point setup. For STM32H735xx, settings should be set as below.

cmake
set(CPU_PARAMETERS
    -mthumb
    -mcpu=cortex-m7         # Set Cortex-M CPU
    -mfpu=fpv5-d16          # Set Floating point type
    -mfloat-abi=hard        # Hardware ABI mode
)

General rule for settings would be as per table below

STM32 Family -mcpu -mfpu -mfloat-abi
STM32F0 cortex-m0 Not used soft
STM32F1 cortex-m3 Not used soft
STM32F2 cortex-m3 Not used soft
STM32F3 cortex-m4 fpv4-sp-d16 hard
STM32F4 cortex-m4 `fpv4-sp-

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ccmakecortex-debugcpptoolsctools

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具