Skip to main content
Gaston
Associate III
March 11, 2022
Question

C++ Project that creates C main file

  • March 11, 2022
  • 13 replies
  • 7137 views

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

This topic has been closed for replies.

13 replies

KnarfB
Super User
March 11, 2022

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
March 11, 2022

"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
GastonAuthor
Associate III
March 12, 2022

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

Pavel A.
March 12, 2022

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.

Gaston
GastonAuthor
Associate III
March 13, 2022

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

Pavel A.
March 14, 2022

> In fact it is the opposite. Using extern C allows you to call C functions from C++.

And also declare C++ functions callable from C and global variables that can be referred from C.

Gaston
GastonAuthor
Associate III
March 14, 2022

I think we should better understand how this works:

https://www.geeksforgeeks.org/extern-c-in-c/

My suggestion is clear and I have also provided the case of MCUXpresso, an environment very similar to STM32Cube, which solves this issue easy and efficiently.

Please, any feedback from ST side?