Default Handlers in C: weak_alias function tells the linker that new
is to be a weak alias for old
. That is, this definition of new
is a weak symbol. If there is no other definition of a symbol called new
, this old
definition stands.
Might seems alien to you first, so go through a below example & read again.
- Definition of
weak_alias
is as follows :
- If there is another (non-weak) definition of
new
then that non-weak(i.e. strong) definition stands and the weak definition is ignored.
Let’s understand default handlers in C: weak_alias by example
oldDef.c
weak.c
Compilation
- If you run the above program as it is, it will print
- But if you uncomment
Feature1()
then it will print
- Why so? It’s due to way linker understand symbols. When you first run without
Feature1()
function linker does not found strongFeature1()
symbol so it links toDefaultHandler()
And in the second case, when we introduceFeature1()
linker finds a strong symbol & links it toFeature1()