Skip to main content
Akuks.1
Associate
August 19, 2022
Solved

SWV ITM (printf) is not working with main.cpp(c++) but with main.c it works like a charm.

Okay basically i have two question,

[1]

SWV ITM (printf) is not working with main.cpp(c++) but with main.c it works as a charm. The culprit seems to be is main.cpp but i have no idea how to make it work there.

[2]

 The project is converted to C++ even though it produces main.c files instead of main.cpp. So i have to manually rename it to main.cpp . When ever i make any changes to .ioc the cubeMx inside of the cubeIDE produces the main.c instead of a c++. Is there any way to automatically generate source files in cpp extension because manually renaming it often leads to human error. Thank you.

This topic has been closed for replies.
Bob S
Bob SBest answer
Super User
August 19, 2022

Sure you can. You could, for example, create something like "appmain.cpp" with a function "appmain(void)". In appmain.h you provide the function prototype:

#ifdef __cplusplus
 extern "C" {
#endif
 
void appmain( void );
 
#ifdef __cpluscplus
 }
#endif

Then from main(), call your appmain() to then call C++ functions, instantiate class instances, etc.

3 replies

Pavel A.
August 19, 2022

If you just leave the generated main.c as is, both self-inflicted issues would disappear.

Akuks.1
Akuks.1Author
Associate
August 19, 2022

Thats very basic, but couldn't do it as the project has a library that was written in c++.

Bob S
Bob SBest answer
Super User
August 19, 2022

Sure you can. You could, for example, create something like "appmain.cpp" with a function "appmain(void)". In appmain.h you provide the function prototype:

#ifdef __cplusplus
 extern "C" {
#endif
 
void appmain( void );
 
#ifdef __cpluscplus
 }
#endif

Then from main(), call your appmain() to then call C++ functions, instantiate class instances, etc.