cancel
Showing results for 
Search instead for 
Did you mean: 

C++ Project that creates C main file

Gaston
Senior

Hi,

If I create a C++ STM32CubeIDE project the main generated file is a C file (main.c) and I have to rename this to main.cpp every time I update pinmux, drivers, etc in order to call C++ functions. I think it's not an usual workflow but could anyone confirm this?

gaston

13 REPLIES 13
KnarfB
Principal III

Confirmed. You may add a BUG topic to this post if you like.

As a workaround, create your own .cpp files with a main_cpp() and call it from main() in a user code section.

hth

KnarfB

TDK
Guru

"C++ project" means it has the C++ compiler step in addition to assembly and C. As if, if you add *.cpp files, it will handle them appropriately. Not sure if creating main.c is a bug or intended here.

If you feel a post has answered your question, please click "Accept as Solution".
Gaston
Senior

I'm not sure if it's a bug or not but the MCUXpressoIDE works as expected. In this case If you create a C++ project the IDE generates a .cpp main file (MKE06Z4_Project.cpp). This is a natural workflow to me without doing workarounds.

0693W00000KcLyhQAF.pnggaston

Please be sure, this is not a bug.

TL;DR Most of the ST libraries called by cube-generated code are plain C so it is just natural that the main.c is also a plain C file.

Perhaps the choice of the filenames main.c/main.h was not far-sighted, but it's too late to change. Many people already are used to this and will be upset if they change it.

OTOH integration of this C code with c++ is very easy (by design of c++).

For example, you can name your "main" cpp file like in MCUXpresso: "your_Project.cpp", change there main() to cpp_main(), and be done with it.

In this case it may be more appropriate to speak of 'very' nice-to-have feature.

The MCUXpresso libraries are also written entirely in plain C and if you create an MCUXpresso project in C++ you can call them from main.cpp without limitations. On the other hand, if you create a C project you won't be able to call libraries/functions written in C++.

Basically, creating a project in C++ should allow calls to C/C++ functions/libraries from main.cpp while a project created in plain C (main.c) will only allow calls to library functions written in plain C. I think no one will feel upset about this.

Despite creating a C++ project the STM32CubeIDE only generates a plain main.c instead of main.cpp. This makes it impossible to call C++ functions/libraries from main.c. I would have no problem renaming the file once but if I do multiple pinmux/peripherals updates they all generate a main.c and this workflow is no longer efficient at all.

gaston

You can call C++ functions from C using extern C. The choice is not as clear cut as you make out to be.
If you feel a post has answered your question, please click "Accept as Solution".

You're right, but looking at this from the Cube team POV, they would need to add a big feature - C++ code generation and all the templates; with all due validation and testing.

Is it really worth it?

C is not a broken C++, so there isn't anything to fix.

Maybe the old school, yep. I don't feel a slightest remorse from some C files in a C++ project. It works together cleanly, by design.

More to this... C++ is not 100% backward compatible with C (even "modern"), there are subtle incompatibilities. Compiling ST headers with C++ can cause at least a lot of warnings.

So for them adding C++ code generation is a non-nonsense effort with unclear reward.

@TDK No, you can't. In fact it is the opposite. Using extern C allows you to call C functions from C++.

@Pavel A.​ It is much easier than it seems. If you create a project in C++, the STM32CubeIDE simply has to generate a main file with the extension cpp instead of c. The rest of the code stays the same as in C and you don't need to do anything else. Then pinmux and peripheral updates modify the main.cpp file and voila. Everything flows. At least that's how MCUXpressoIDE does it. I don't know if other IDEs do the same, but it is true that many offer little support for C++.