2026-04-25 9:44 PM - last edited on 2026-04-30 2:38 AM by Andrew Neil
In STM32CubeIDE, I can compile C++ code in the project. The CMake file generated for the VSCode extension doesn't work with C++. Is there a way to use C++ code with less hassle than trying to get around this by hand?
Solved! Go to Solution.
2026-04-25 9:55 PM
C++ is supported for free.
CMake itself is agnostic. It relies on file(s) extension(s) to pass right compiler settings.
STM32Cube for Visual Studio Code requires no trick nor magic to get C++ running.
2026-04-25 9:55 PM
C++ is supported for free.
CMake itself is agnostic. It relies on file(s) extension(s) to pass right compiler settings.
STM32Cube for Visual Studio Code requires no trick nor magic to get C++ running.
2026-04-25 10:21 PM
Ok. So You're saying that if I follow the typical process:
Then I should be able to make my main.c a main.cpp, and it will compile?
2026-04-26 7:15 AM
Please explain how you would normally configure the project to be able to add c++ classes and include them in main from Visual Studio Code.
2026-04-27 1:46 PM
@jsteezy
STM32CubeMX is only generating C projects. C++ has to be your own addition. Maybe https://isocpp.org/wiki/faq/mixing-c-and-cpp
2026-04-29 2:50 PM - edited 2026-04-29 2:52 PM
Hi @jsteezy
maybe my little demo project will be helpful: https://github.com/TimoEngelmann/nucleo-h753zi-cpp-demo
In the file .\Application \application.cpp you can find two C++ functions:
void Setup(void) This function is called once after startup.
void Loop(void) This Function ist called from the infinite loop in main.c file.
If you are starting from scratch (i.e. if you have a different board) follow these steps:
1. STM32CubeMX:
- Open STM32CubeMX.
- Select your board or controller.
- Configure all
- Project Manager / Project / Toolchain/IDE = CMake and Default Compiler/Linker = ST Arm Clang.
- Click GENERATE CODE button
2. Copy the following parts from my demo project to your project:
- File: STM32Project_open_in_VSCode.code-workspace
- File: .clang-format (optional if you need the formatter settings)
- Folder: Application
- Folder: .vscode
3. Open the generated file main.c and insert the following:
The two prototype lines:
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
extern void Setup(void);
extern void Loop(void);
/* USER CODE END PFP */The two function calls:
/* Infinite loop */
/* USER CODE BEGIN WHILE */
Setup(); // Calls the C++ main function in ./Application/application.cpp.
while (1)
{
Loop(); // Cyclic call of C++ loop function in ./Application/application.cpp.
/* USER CODE END WHILE */
4. Open the generated file CMakeLists.txt and insert the following:
Add the last two lines to the end of the file:
# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
# Add user defined libraries
)
# Add Application/CMakeLists.txt
add_subdirectory(Application)
5. Adapt the application.cpp file:
- Replace the controller specific include with your own: #include "stm32h7xx_hal.h"
- If you have not configured a USART, you have to remove all the printf() lines from the application.cpp file.
- If you have configured a USART, copy (and possible adapt) the usart.c file. The function int __io_putchar(int ch) must be implemented in this file..
- If your pins are different, replace them or remove the lines.
6. Open the Workspace (via STM32Project_open_in_VSCode.code-workspace file) in VSCode
- Wait until the project is recognised by the ST extension and follow the on-screen instructions.
- Go to the command bar and type: > CMake: Delete Cache, Reconfigure and Build
- Go to the Run and Debug sidebar and start the debugger.
I hope this was helpful to you or others.
Best regards,
Timo