Search code examples
c++armembeddedstartupkeil

Transform Keil5 C Project to C++ Project Error with the Interrupt Function


After I transform the Project from C to C++, the program is seem to be stuck in the interrupt function.

I've wrote a keil embedded project with C, and then I tried to write it with C++. Then I found the program was stuck in the interrupt function even I write the IRQHandler functions properly (they do work in C project). The USART1 will be stuck meanwhile the CAN receive/transmit are still working.

// this is the USART_IRQHandler   

void USART1_IRQHandler(void)      
{
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)   
    {                                                                        
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}

Solution

  • The interrupt handlers require C linkage if using C++ compilation:

    extern "C" void USART1_IRQHandler(void)      
    {
        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)   
        {                                                                        
            USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        }
    }
    

    Without C linkage the function USART1_IRQHandler will not replace the default handler of the same name but with C linkage which is an endless-loop, hence it appears to be "stuck in the interrupt function", which it is... just a different interrupt function.

    The C linkage prevents C++ "name mangling" that otherwise causes the symbol to not be the same as the interrupt handler weak-link symbol name.