cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX / C++ - Linkage Error in generated Code

smrtkai
Senior
Posted on June 26, 2017 at 11:49

Hi,

I want to use STM32CubeMX and I would like to program my new project in C++. I am using the latest version of STM32CubeMX (4.21.0) and the latest version of Keil uVision (5.32.0.0).

After code generation I have added the compiler flag '--cpp'. I haven't added any custom code yet. But when I compile the project I get the following error. Without the compiler flag everything compiles and links without an error or warning.

compiling main.c...

../Inc/adc.h(62): error:  &sharp337: linkage specification is incompatible with previous '_Error_Handler'  (declared at line 162 of '../Inc/main.h')

  extern void _Error_Handler(char *, int);

The function declarations for '_Error_Handler' seems to be correct. In main.c the function declaration is included from main.h and from every peripheral-header. In main.h the declaration does not have an 'extern' specifier. That seems to be the only difference. When I remove the function declaration for '_Error_Handler' in main.h, it works.

But is that the correct way of programming in C++ with STM32CubeMX projects? If so, I would need to remove the declaration of '_Error_Hanlder' every time I re-generate the code.

#c++ #cpp #stm32cubemx*
3 REPLIES 3
Nicolas Cmb
Associate III
Posted on June 26, 2017 at 11:57

I don't know if keil is able to change the programm into C++ but I know that Eclipse can use C and also C++ language

Posted on June 27, 2017 at 14:24

Keil does support C++. You have to add the compiler-flag C++. But the generated code from the current STM32CubeMX version seems to have a problem with C++.

If I comment out the Error_Handler declaration in main.h everything compiles and links. But with the declaration 'void _Error_Handler(char *, int);' in main.h there is a linkage error.

Oros Nig
Associate II
Posted on August 23, 2017 at 22:59

Hi,

Error_Handler() interface present in main.h is not specified to be used in C-style linkage as it's done in the rest of the code.

Add

#ifdef __cplusplus extern 'C' { #endif

_Error_Handler(char *, int);  

#ifdef __cplusplus}#endif

in the main.h to resolve the problem.

see

https://community.st.com/0D50X00009XkXpXSAV

Nig Oros