2024-12-07 03:01 AM - last edited on 2024-12-09 05:10 AM by SofLit
in default situation, you could not call c++ function in C file.
So the main.c is not suitable file in C++ project.
if user choose C++ project,the generated main.c ,stm32h7xx_it.c should sufixed with .cpp.
2024-12-09 01:49 AM
Hello @yujin
Please check this C++ Project on STM32CubeIDE - STMicroelectronics Community
and There is a plan to allow STM32cubeMX for generatin... - STMicroelectronics Community ,
it may help you solve the issue .
THX
Ghofrane
2024-12-09 02:13 AM
@yujin wrote:in default situation, you could not call c++ function in C file.
Did you provide the appropriate extern "C" declaration?
https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c
2024-12-09 02:32 AM - edited 2024-12-09 02:37 AM
main.c:
extern "C" void c_function_in_cpp_file();
int main(void)
{
c_function_in_cpp_file();
while(1)
{
}
}
cpp_file.cpp:
void cpp_function_in_cpp_file()
{
}
extern "C" void c_function_in_cpp_file()
{
cpp_function_in_cpp_file();
//function can be linked to C code, but can execute C++ code
}
You make a header file compatible with both C and C++ by using:
cpp_file.h:
#ifdef __cplusplus
//c++ class definitions etc. invisible to C files including this header
#endif
#ifdef __cplusplus
extern "C" {
#endif
//functions that can be called by both C and C++
#ifdef __cplusplus
}
#endif