cancel
Showing results for 
Search instead for 
Did you mean: 

Can CubeMX generate C++ code or is this feature planned?

fsch
Associate III
 
5 REPLIES 5
OJack.1
Associate

I would also like to know this, I'm using a library that is C++ and you can change the main.c to main.cpp but if you have some functionality in main.cpp already it's a bit of a pain..

I guess the work around would be to change the cpp file to a c file then changing it back to a cpp file using F2(rename) after you have made the changes in .ioc

When creating your project, choose C++ as targeted language for your STM32 project.

Add 2 new files to your project. maincpp.h and maincpp.cpp. In maincpp.h you include main.h and declare two extern "C" functions: setup() and loop() which you implement in maincpp.cpp using C++. In main.c include maincpp.h and call setup() in a user code section right before the main loop and loop() in a user code section within the main loop. Now you have separated generated C code from your own C++ code.

Note that C++ may generate more code and some C++ libs use dynamic memory heavily which might cause issues.

FS.1
Associate III

Hi,

I am using this approach even vor my C projects at work. I have an appmain.c/h and just call their app_init() and app_loop() from CubeMX's main.c.

I still need to test this with C++ at home.

And of course you need to be careful with what C++ features you use. E.g. most of the STL data structures are off the table ... There are some people writing or talking about this topic on the internet. ... I hope I can get my hands on that topic for real soon. :)

Cheers