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
This tutorial is using Windows operating system. Similar procedure will apply for Linux and MAC operating system.
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
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:
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\c:\ST\STM32CubeIDE_1.0.2\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.stlink-gdb-server.win32_2.0.100.202109301221\tools\bin\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

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.
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

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

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++ developmentms-vscode.cmake-tools: CMake core tools, build system generator tooltwxs.cmake: CMake color highlightingmarus25.cortex-debug: Cortex-M debugging extension, mandatory for STM32 debug from VSCodedan-c-underwood.arm: ARM Assembly syntax highlighterzixuanwang.linkerscript: GCC Linker script syntax highlighterYou 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.

At this point, all the tools are properly installed - you are on the right track towards success.
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.
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.
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.txtfile is sometimes called top-level CMake file
Essential things described in CMakeLists.txt file:
-I)-D)Visual Studio Code has been installed and will be used as further file editor.
Find your generated project path and open folder with VSCode:
Open in Code.File -> Open Folder... to open foldercode .Final result should look similar to the one below

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:
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
# Some default GCC settings
# arm-none-eabi- must be part of path environment
set(TOOLCHAIN_PREFIX arm-none-eabi-)
set(FLAGS "-fdata-sections -ffunction-sections --specs=nano.specs -Wl,--gc-sections")
set(CPP_FLAGS "-fno-rtti -fno-exceptions -fno-threadsafe-statics")
# Define compiler settings
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc ${FLAGS})
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++ ${FLAGS} ${CPP_FLAGS})
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size)
set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_C ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
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.
We need to create main CMakeLists.txt, also called root CMake file.
Make sure you really name it
CMakeLists.txtwith 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.
```cmake cmake_minimum_required(VERSION 3.22)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_EXTENSIONS ON) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS ON) set(PROJ_PATH ${CMAKE_CURRENT_SOURCE_DIR}) message("Build type: " ${CMAKE_BUILD_TYPE})
project(your-project-name) enable_language(C CXX ASM)
set(CPU_PARAMETERS -mthumb
# This needs attention to properly set for used MCU
-mcpu=cortex-m7
-mfpu=fpv5-d16
-mfloat-abi=hard
)
set(linker_script_SRC ${PROJ_PATH}/path-to-linker-script.ld) set(EXECUTABLE ${CMAKE_PROJECT_NAME})
set(sources_SRCS # Put here your source files, one in each line, relative to CMakeLists.txt file location )
set(include_path_DIRS # Put here your include dirs, one in each line, relative to CMakeLists.txt file location )
set(symbols_SYMB # Put here your symbols (preprocessor defines), one in each line # Encapsulate them with double quotes for safety purpose )
add_executable(${EXECUTABLE} ${sources_SRCS})
target_include_directories(${EXECUTABLE} PRIVATE ${include_path_DIRS})
target_compile_definitions(${EXECUTABLE} PRIVATE ${symbols_SYMB})
target_compile_options(${EXECUTABLE} PRIVATE ${CPU_PARAMETERS} -Wall -Wextra -Wpedantic -Wno-unused-parameter # Full debug configuration -Og -g3 -ggdb )
target_link_options(${EXECUTABLE} PRIVATE -T${linker_script_SRC} ${CPU_PARAMETERS} -Wl,-Map=${CMAKE_PROJECT_NAME}.map --specs=nosys.specs -u _printf_float # STDIO float formatting support -Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-group -Wl,--print-memory-usage )
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${CMAKE_SIZE} $ )
add_custom_command(TARGET ${EXECUTABLE
$ claude mcp add stm32-cube-cmake-vscode \
-- python -m otcore.mcp_server <graph>