A link is the process of collecting and combining a wide variety of code and data departments into a single file that can be loaded (or copied) into memory and executed. Links can be executed at compile time, that is, when the source code is translated into machine code, or when the facilitates is executed, that is, when the program is loaded by the loader and executed, or even executed at run time by the application.
1 Compiler Driver
Consider the following two source files: Main.c and swap.c.
Main.c
void swap (); int buf[2]={1,2}; int Main () { swap (); return 0 ;}
Swap.c
extern int buf[]; int *bufp0=buf; int *bufp1; void swap () { int temp; BUFP1=&buf[1]; Temp=*bufp0; *bufp0=*bufp1; *bufp1=temp;}
Most compiled systems provide a compiler driver, which represents the language preprocessor, compiler, assembler, and linker that the user invokes when needed.
For example:
Gcc-02-g--o P MAIN.C swap.c
The driver first calls the C preprocessor (CPP), which translates the C source program main.c into an intermediate file main.i:
CPP [Other arguments] main.c/tmp/main.i
Next, the driver runs the C compiler (CC1), which translates main.i into an assembly-language file Main.s
Cc1/tmp/main.i main.c-02 [Other arguments]-o/tmp/main.s
The driver assembler (AS) then translates the main.s into a relocatable target file main.o:
As [other arguments]-o/tmp/main.o/tmp/main.s
The driver generates SWAP.O through the same process. Finally, it runs the linker program LD, which combines MAIN.O and SWAP.O with some of the necessary system target files to create an executable target file P:
Ld-o p [System object files and args]/tmp/main.o/tmp/swap.o
To run the executable p, we just need to enter./P
The shell invokes a function called the loader in the operating system, which copies the code and data from the executable p to the memory, and then transfers the control to the beginning of the program.
2 static links
In-depth understanding of computer system architecture--Links