The weak function attribute causes the symbol resulting from the
function declaration to appear in the object file as a weak symbol, rather
than a global one. The weak attribute can also be applied to
variables. The language feature provides the programmer writing library
functions with a way to preempt duplicate name errors if the user overrides
the function definition in his or her code.
The weak function attribute follows the general syntax for function attributes. The following diagram shows the supported forms.
>>-__attribute__--((--+-weak-----+--))------------------------->< '-__weak__-'
Normally, when several relocatable object files are processed, the linker disallows multiple definitions of global symbols with the same name. However, the linker allows a weak definition in the presence of a global symbol with the same name; the weak definition is ignored. Another difference between a global and a weak symbol lies in whether the linker searches archive libraries. To resolve undefined global symbols, the linker searches archive libraries and extracts members that contain definitions; it does not do this to resolve undefined weak symbols.
The following restrictions and limitations apply to weak symbols:
Related References