Interrupt handlers and the C++ compiler [SOLVED]

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Interrupt handlers and the C++ compiler [SOLVED]

719 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fjrg76 on Wed Dec 24 16:58:15 MST 2014
Hi,

I faced an issue for the interrupt handlers while compiling my C++ application. I solved it already, but I want to post my solution in case you'll find yourself struggling with the same problem.

General solution:

When mixing C and C++ code for the same project one needs to place the C code in between

#ifdef __cplusplus
extern "C" {
#endif

/* here your C functions (prototypes or declarations */

#ifdef __cplusplus
}
#endif


Particular solution:

What I did was to embrace the interrupt handler in between the preprocessor directives

#ifdef __cplusplus
extern "C" {
#endif  
void SysTick_Handler(void)
{
if (sleep.IsRunning()) {
--sleep;
if (sleep.Get() < 1) {
sleep.Stop();
}
}
}
#ifdef __cplusplus
}
#endif  


Otherwise the compiler will use the default SysTick_Handler():

__attribute__ ((section(".after_vectors")))
void SysTick_Handler(void)
{ while(1) {}
}


Why is so? Because C++ adds some characteres to the name of the functions (or methods). So, for the compiler (if you don't use the preprocessor guards) the function
SysTick_Handler looks like __SysTick_Handler__ (or something similar), which is naturally very different to what you expect. Using the preprocessor guards you're compeling the compiler to respect the name (among other things).

Hope this post is helpful for you.
Labels (1)
0 Kudos
1 Reply

660 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Dec 24 17:03:47 MST 2014

Quote: fjrg76
Why is so? Because C++ adds some characteres to the name of the functions (or methods).



That's 'Name mangling'  :)

See. http://en.wikipedia.org/wiki/Name_mangling
0 Kudos