Parsing the reference process
During the symbol parsing phase, the linker scans the target file and saves it to a file in the order in which they appear on the compiler driver command line, from left to right.
In this scan, the linker maintains a set of relocatable target files E(the files in this collection are the module files that are determined to be finally merged into the executable file).
An unresolved (that is, a symbol that is referenced but not yet defined) collection U, and a parsed (previously defined but not referenced) collection D.
For each file f on the command line, the linker determines whether F is a target file or an archive file.
1. Determine the type of input file
If the target file
The linker will add this file to set E and modify the state of the collection U and D according to the symbol reference. The next file is then processed.
If it is an archive file
The linker will attempt to match the symbols of unresolved symbols and archive file member definitions in the collection U, and if the member m of the archive file defines a symbol to resolve a reference in U,
So long add m to the set E, and then modify the states of U and D. Repeat this process for each member of the archive file until you and d are no longer changing, and then simply discard
The member target file that is not contained in collection E. The linker then continues to process the next file.
2. Determine if the set U is empty
If the linker finishes scanning the file on the command line and the collection U is still not empty , then the linker will error and terminate the program if it references an undefined symbol.
If the linker finishes scanning the file on the command line, the collection U is still empty, and the target file in E is merged and relocated, and the executable file is output.
Note the order of the libraries and destination files on the command line
The library is typically placed at the end of the command line
1. If the libraries are independent of each other, you can place them at the end of the command line in any order.
2. If the libraries are interdependent relationships, they must be sorted so that the symbol s, which is referenced externally to each member of the archive file, has at least one S definition in the command line after the reference to S.
How the linker uses static libraries to resolve references