cancel
Showing results for 
Search instead for 
Did you mean: 

Main.c and main.cpp when mixing C and C++

David_
Senior

We created an empty C++ project.  Then, added some web-sourced .cpp and .hpp code into the directory. Compiled and debugged with ST-Link MiniE fine.  Entry is main.cpp

In STM32CubeIDE, click .ioc and edit init code with STM32MX.  STM32MX generates main.c instead of main.cpp where our code resides. 

Many thanks

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

This is the intended behavior. There is no problem with C and C++ files in the same project, and HAL is designed in C.

If you want to run a C++ function from within main.c, define it with extern "C" and call it normally. Calling a C++ main loop function right before the main loop in main.c is common.

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

View solution in original post

1 REPLY 1
TDK
Guru

This is the intended behavior. There is no problem with C and C++ files in the same project, and HAL is designed in C.

If you want to run a C++ function from within main.c, define it with extern "C" and call it normally. Calling a C++ main loop function right before the main loop in main.c is common.

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