Skip to main content
martin salko
Associate II
May 26, 2023
Solved

Generating c++ projects with STM32CubeMX

  • May 26, 2023
  • 2 replies
  • 7464 views

Hi, I'm trying to make a c++ project for STM32CubeIDE using the STM32CubeMX.

However there doesn't seem to be any c++ option, is there something I'm missing or is the software just not made for it?

This topic has been closed for replies.
Best answer by Pavel A.

After opening the project in CubeIDE right click on it and select "Convert to C++".

This won't change the code generated by CubeMX to C++. All these files remain C, this is normal, you don't want to change it. Use C++ in source files that you add.

2 replies

martin salko
Associate II
May 26, 2023

Ok so my question still stands, however there's a way around it? you can make new project from existing ioc file, which lets you make a c++ project.

it's just kinda upside down approach ;-;, maybe you're not supposed to use cubemx anymore? this all is very confusing.

Pavel A.
Super User
May 26, 2023

CubeMX simply is not aware of C++. It always creates its stuff in plain C, and it works in both C and C++ projects. Ability to co-exist with C code is a fundamental feature of C++.

Pavel A.
Pavel A.Best answer
Super User
May 26, 2023

After opening the project in CubeIDE right click on it and select "Convert to C++".

This won't change the code generated by CubeMX to C++. All these files remain C, this is normal, you don't want to change it. Use C++ in source files that you add.

martin salko
Associate II
May 26, 2023

Yeah that is fair. The thing is that makes all this really annoying is it generates main.c. and if I'd want to make a project in which I can change stuff in CubeMX, I can't turn it into main.cpp.

So now in order to just use any of my existing libraries I have to have main.c which doesn't contain main function and main.cpp which does.

Or have a c wrapper for c++ code which also isn't as nice.

Ig wanting the main.c to just change to cpp is too much.

Pavel A.
Super User
May 27, 2023

Just add your own cpp file(s) where you use these existing c++ libraries.

The main function remains in the main.c file, it is already good as it is.

Any global variable defined in main.c and other generated c files can be easily reached from cpp files via extern "C", as you of course know.

C wrapper for c++ code? Why this may be needed? Again, in most cases a simple extern "C" is enough.

> wanting the main.c to just change to cpp is too much

Short answer - yes, too much )) This does not add real value but creates extra work to others.