cancel
Showing results for 
Search instead for 
Did you mean: 

C++ main.cpp generation

VShap.1.358
Associate II

Dear ST,

Do we have a functionality to generate main.cpp from CubeMX? HAL supports C++ and it is possible to make project C++ by renaming main.c to main.cpp, but after regeneration you just get new main.c without your code, so again need to merge it manually with existing main.cpp. This is very annoying and would be very useful to be able to generate file main.cpp in CubeMX and CubeIDE directly.

Do we have an easy solution to fix it? If not, is it planned to be implemented in user friendly way in CubeMX?

Thank you.

4 REPLIES 4
Piranha
Chief II

Rename the main() function in main.cpp file to some other name and call it from generated main.c file.

Ozone
Lead II

I neither work with Cube, nor with C++. The latter only because our module test system does not support it.

However, it is usually not that simple.

C++ requires a different startup, which includes contructor calls. Default C startup code does not.

And second, the name overloading feature requires a different linker and linking strategy.

Piranha
Chief II

C++ was designed to be backwards compatible with C. Different linking is solved like described in this topic. And __libc_init_array() does call constructors with __preinit_array_start() and __init_array_start() calls.

VShap.1.358
Associate II

Hi colleagues,

thanks for your answers.

In fact, if main file is named as main.cpp, project is considered as C++ and liker uses correct strategy. All works fine. I've found a way how to work - rename main.c to main.cpp, develop code. In case you need to add new peripheral and start code generation, rename back to main.c, generate, and return back cpp extension. This way you save a code and will not need to merge. Stupid, but probably best way now... I mean, would be perfect in case cube just generate main with .cpp or .c extension depending on setting (one more selection feature in project tab).

Also there is an option in CubeIDE to select project as C or C++, or even in existing project - right click - turn to C or C++. This option exist, but it do not do anything, no effect... Or I do not understand how it works.

Or indeed use approach of Piranha as another workaround, but it impacts project structure.

Regards,

Volodymyr.