cancel
Showing results for 
Search instead for 
Did you mean: 

I cant define a class in uV5

T J
Lead
Posted on December 25, 2016 at 02:00

I set all the pins in CubeMx,

added a class to a .h file

renamed main.c to main.cpp

included the .h

and 'class' is undefined.

is there a switch in Cube for this ? 

using an '091

#class-undefined #uv5 #keil
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on December 25, 2016 at 02:52

Needs to be a .CPP file that does the including, or you need to add the --cpp to enable compiler support. ie compile .C as C++

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5
Posted on December 25, 2016 at 02:52

Needs to be a .CPP file that does the including, or you need to add the --cpp to enable compiler support. ie compile .C as C++

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 25, 2016 at 02:57

Hey it worked.

Project options c/c++ 

in Misc Controls

add --cpp

Posted on December 25, 2016 at 03:03

One has to watch for C++ name mangling, functions you wish to export to the vector table, for example, need extern 'C' prefix so they bind properly in the linker, rather than the WEAK ones. The SysTick Handler being one to watch in CubeMX/HAL

ie

extern 'C' void USART1_IRQHandler(void)

{

 ...

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 25, 2016 at 03:10

I am a new C programmer

:(

do you mean in the .cpp file :

extern 'C' void USART1_IRQHandler(void)

{...}

and in the .h file:

extern 'C' void USART1_IRQHandler(void);

I am totally unsure about the syntax/implementation of your advice.

Posted on December 25, 2016 at 13:33

Yes, for functions that must be called from C or assembler, and when compiled as C++, you must use this syntax so the name is exported cleanly, otherwise it will be mangled. The name mangling process used by the compiler causes a unique name based on the parameters input to and returned by a function.

https://en.wikipedia.org/wiki/Name_mangling

 

http://www.geeksforgeeks.org/extern-c-in-c/

 

In the STM32 case what would happen is that the weak/default functions would get linked into the vector table if the functions you compiled as C++ remained mangled. The result would be that your interrupts would go into the Default_Handler and get stuck in a while(1) loop, instead of the IRQHandler body code you supplied.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..