Select Page

Tip for STM32 developers: Convert your project from C to C++ with STM32CubeMX and Open System Workbench for STM32

We have compiled this tip for developers who work with the Workbench for STM and want to compile their project with C++.

Generate The C code in the STM32CubeMX according to the hardware configuration for the System Workbench for STM32. C++ code generation is not supported here.

STM32CubeMX Code Generation - Project Settings

Image 1: STM32CubeMX Code Generation – Project Settings

Open the created project with the System Workbench for STM32 (e.g. by double-clicking on the .project (File in the generated project folder).

Create The project. Error messages may still occur.

Convert Convert the C project to a C++ project using the System Workbench for STM32; use the context menu to do this.

STM32CubeMX project context menu in the project browser

Image 2: Project context menu in the project browser

Check The C++ project settings (normally nothing needs to be changed). The path settings are automatically copied from the previous C settings.

STM32CubeMX - C++ Project Settings

Image 3: C++ Project Settings

Name in the project browser main.c in main.cpp um.

STM32CubeMX - Project Browser

Image 4: Project browser view

Add the following lines in main.h in addition:



#ifndef __MAIN_H

#define __MAIN_H

#ifdef __cplusplus

 external ""C" {

#endif



#ifdef __cplusplus }

#endif

#endif /* __MAIN_H */

Image 5: Code view main.h

Add the following lines in main.cpp in addition:

… /* Private function prototypes -----------------------------------------------*/

#ifdef __cplusplus

external ""C" {

#endif

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

#ifdef __cplusplus }

#endif

Image 6: Code view main.cpp

Clean the The project and its mistakes.

Create The project; no more errors should occur.
However, any potential warning signs should still be checked.

Further information

MicroConsult Training & Coaching on the topic of microcontrollers

MicroConsult Training & Coaching for C++ Programming

MicroConsult expertise on the topic of microcontrollers

MicroConsult Newsletter

With the MicroConsult newsletter, you'll stay on the pulse of the embedded world. Look forward to proven practical knowledge, real professional tips, and current events – directly from our experts for your project success.

Subscribe now!

Published by

Thomas Batt

Thomas Batt