cancel
Showing results for 
Search instead for 
Did you mean: 

Generating c++ projects with STM32CubeMX

martin salko
Associate II

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

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.

View solution in original post

6 REPLIES 6
martin salko
Associate II

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

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.

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

ok but this still makes a main.c file?

so i still have to make leaps to actually add a cpp file in that would compile properly?

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.

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.