The generation of an executable target file needs to be precompiled (g++-E), compiled (g++-s), compiled (g++-c), and linked four steps. A link is the process of merging multiple relocatable target files into a single destination file. The link is done by the linker and the linker is going to complete two tasks:
1, the symbol (symbol table by the assembler constructs) parses. That is, associating symbolic references with symbolic definitions. where symbol definitions and symbol references may not be in the same destination file. and the linker has some rules for parsing global symbols for multiple definitions: it is not allowed to have more than one strong symbol with the same name (functions and initialized global variables), if there is a strong sign and multiple weak symbols with the same name, select a strong symbol, if there are multiple weak symbols randomly selected one.
2, reposition. This includes merging the corresponding sections of different relocatable target files into a single section, and repositioning symbol references so that references to symbols point to the correct run-time addresses.
1. Generate a static library
# g++-C a.cc b.cc
# g++ ar RCS LIBAB.A a.o b.o
# g++-static-o prog main.o./LIBAB.A
With this form, multiple target modules can be packaged into a single file, called a static library. When linking to a static library, only the target module referenced by the program is copied, but it is important to note that the order in which the files appear on the command line must be correct when the link forms the final executable file. That is, the symbol definition must appear after the symbol reference. So the library is generally placed at the end of the command line.
Static linking has the following disadvantages:
Multiple processes refer to the same target module, there will be multiple copies in memory, wasting memory
If the static library has an update, you need to relink.
Dynamic links are divided into load-time links and run-time links.
At load time, the linker replicates some relocation and symbol table information during the link phase, while the text and data of the dynamic library are relocated to a memory segment at load time, when multiple processes use the same dynamic library, the memory-mapped method is used to share code and data, and only one copy is in memory.
A run-time link is a way to invoke a system function by displaying it.
Run-time Links
Linux static links and dynamic links