Linker
The compiler creates an object file. This file is almost executable. But recall how compilers handle procedure calls: they branch to an address in memory. The problem with the object file is that all these addresses are given starting at 0. That is, they are relative to the specific file. To be executable, the addresses need to be absolute. The compiler marks the addresses for the linker and the linker updates all the addresses to be absolute. This is called relocation. The result is an executable.
1. Static Libraries
Sometimes the object file makes a procedure call to a function, e.g. printf()
that is not defined in the source file. Instead, the function is defined in some static library. The linker copies over the function definition into the executable and sets the address appropriately in the procedure call.
2. Dynamic Linking
Instead of copying the library code into the executable, dynamic linking instead inserts directions for calling the function from the library at runtime. This allows the library to be shared between multiple executables. This also means that changing the library code will not require re-compiling and re-linking any code that use the library, as is necessary in static linking. However, changing the library code can potentially break code that depends on it, which is not a danger in static linking.