lpcware

Interrupt handlers and the C++ compiler [SOLVED]

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
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.

Outcomes