Skip to main content
Associate III
August 17, 2024
Solved

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

  • August 17, 2024
  • 1 reply
  • 1195 views

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

Best answer by TDK

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.

1 reply

TDK
TDKBest answer
Super User
August 18, 2024

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""."