For example,printf
is dynamically linked.
But how does the compiler(gcc) know that?
The linker locates all the undefined references in the libraries. If the library is a static one, the linker just adds the actual machine code to your final executable. On the other hand, if the library is a shared one, the linker only records the name (and version?) of the library in the executable's header.
Dynamic libraries are linked during the execution of the final executable. Only the name of the dynamic library is placed in the final executable. The actual linking happens during runtime, when both executable and library are placed in the main memory.
Overview of the C++ compilation steps: Here, libstdc++ is GNU's implementation of the C++ Standard Library which is automatically linked dynamically to your executable binary i.e. a. out . The ldd command lists the runtime library dependencies. libc is the C Standard library which is also linked dynamically.
The main difference between static and dynamic linking is that static linking copies all library modules used in the program into the final executable file at the final step of the compilation while in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.
gcc doesn't know that. It knows there is a function printf
and it knows how to call it, but the object file it generates contains a call to an unresolved symbol.
The symbol is then resolved by the linker, which is given all your object files and libraries. The linker finds the symbol printf
in a library, and after its combined all the relevant modules, it updates the unresolved calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With