1. Compile preprocessing:
The compiler reads the C source.ProgramTo process the pre-processing commands (starting with #) and special symbols. Preprocessing Commands include three types: macro-defined commands, Conditional compilation commands, and header file containing commands. The header file is used to make some definitions available for multiple different C source programs. In the C source program that requires these definitions, you only need to add the # include statement, instead of re-defining it. The precompiled program addsCodeAdd all the files to the source file to generate the output file.
In addition to the preceding three preprocessing commands, there are also special symbols. Precompiled programs can recognize some special symbols. For example, the line expression in the source program will be interpreted as the current row number in decimal format. File is interpreted as the name of the currently compiled source program.
The work completed by the Pre-compiled program can be said to be a "Replacement" of the source program. After this process, an output file without macro definition, Conditional compilation instructions, and special symbols is generated.
2. Compilation and optimization:
Compile the program by lexical analysis and syntax analysis. After confirming that all commands comply with the syntax rules, translate them into equivalent intermediate code or assembly code.
Optimization may be involved in the process of generating intermediate or assembly code. There are two types of optimization: one kind of optimization involves only the Code itself, mainly deleting public expressions, loop optimization, code extraction, and deleting useless code assignment. Another optimization design is specific computer hardware. For example, how to adjust and optimize the commands based on the features of the hardware execution commands, reduce the length of the target code, and improve the execution efficiency.
After the intermediate code or assembly code is generated, the Compilation Program converts the intermediate code into a sequence of commands of the target machine to obtain the target file corresponding to the source program. The target file stores the machine language code of the target machine equivalent to the source program. The target file generally contains at least two segments: code segment and data segment.
3. Link:
Some target files generated in the second stage correspond to multiple source programs and cannot be executed immediately. Some problems exist. For example, a function in a source file may reference a symbol (such as a variable or function) in another source file. In a source file, a function in a library file may be called, and so on. These problems need to be solved by connecting the program.
The main task of the Connection Program is to connect the target files to each other. That is, it connects the referenced symbol in one file with the definition of the symbol in another file. Make all these target files a whole that can be executed by the operating system.
Supplement: There are two types of linked libraries.
Static Link
In this connection mode, the function code is directly copied to the final executable file. When the program is executed, it is loaded into the virtual address space of the process. The static Link Library is actually one or more target files.
Dynamic Link
In this way, the function code is put in a target file called a dynamic connection library or shared object. The link program only records the name of the shared object and a small amount of key information in the generated executable file. The dynamic connection library can be shared by multiple processes. There is only one instance in the memory during runtime.
Comparison
Using Dynamic Links can make the executable files smaller and save memory when the shared object is used by multiple processes. However, sometimes the system operation is changed or the provincial level does not ensure that the dynamic Connection Library is available and valid.