1) I don't know why I'd need this for a function. Maybe someone else can step in.
2) The linker determines this by going through all object files and checking the symbols inside each object file. I assume that depending on your linker, the exact search order might vary.
For GNU binutils' ld all object files and libraries that appear in the linker command line after the object containing the missing symbol are searched from left to right and the first found symbol is picked.
Example 1:
- a.o -- uses foo(), bar()
- liba -- provides bar()
- libb -- provides foo()
$> ld a.o -la -lb
will result in a.o being searched for undefined symbols. Thereafter ld will go through the libs from left to right to search for these symbols and will find bar in liba and foo in libb.
This may lead to strange problems upon circular dependencies:
Example 2:
- a.o -- uses bar()
- liba -- provides bar(), uses foo()
- libb -- provides foo(), uses bar()
Now, there is a circular dependency between liba and libb and linking will fail:
$> ld a.o -la -lb
because when searching through the undefined symbols in libb, ld will determine that there is no other lib to the right of -lb that provides this symbol. This may be fixed in at least two ways:
1) link liba twice:
$> ld a.o -la -lb -la
2) use ld's grouping feature
$> ld a.o --start-group -la -lb --end-group
In case 2), the grouping tells ld to search through all symbols in all libs belonging to this group.