I am reading code to define the interrupt vector for an STM32 here. For some reason, both __attribute__((weak)) and #pragma weak are used:
void __attribute__((weak)) NMI_Handler(void); /* NMI Handler */ // [line 12]
#pragma weak NMI_Handler = Default_Handler /* NMI handler */ // [line 48]
Somehow it feels that using both __attribute__((weak)) and #pragma weak is redundant.
Can we do without one of them? Is this style standard?
You are right but…
alone can accomplish what is intended. But it would be good to add
__attribute__((weak))to the function prototypes for the following reason –Assume that the file is a huge one with definitions and prototypes spilled all over the file. Now you use an IDE/Editor to jump to the prototype of
NMI_Handlerand when you arrive there and don’t see__attribute__((weak))attached toNMI_Handleryou might as well assume thatNMI_Handleris defined somewhere else and go on a goose hunt and end up nowhere! Now, as they’ve explicitly added__attribute__((weak)), you get an idea that this function’s definition might not be there and you can now search for any pragma aliasing instead of trying to find the actual definition of the function which doesn’t/may not exist!