Transferred from: https://www.cnblogs.com/Lynn-Zhang/p/5377024.html
C + + program compilation process (pre-compilation, compile-so links)
Basic process of the program
1. Preprocessing
Preprocessing is equivalent to assembling a new C + + program based on preprocessor directives. After preprocessing, will produce a no macro definition, no conditional compilation instructions, no special symbols of the output file, the meaning of this file is the same as the original file, but the content is different.
① Remove All "#define" and expand all macro definitions
② handles all conditional compilation directives, such as "#if", "#ifdef", "#elif", "#else", "endif", and so on. The introduction of these pseudo-directives allows programmers to define different macros to determine which code is processed by the compiler. The precompiled program will filter out unnecessary code based on the relevant files.
③ handles the "#include" precompiled instruction, inserting the contained file into the location of the precompiled instruction.
(Note: This process may be recursive, meaning that the included file may also contain other files)
Delete all comments
Add line number and file name identification.
To facilitate compile-time compiler-generated line number information for debugging and to display line numbers when compiling errors or warnings generated at compile time
Keep all #pragma compiler directives
2. Compiling
After a series of lexical analysis, syntactic analysis, semantic analysis and optimization, the files of pre-processing are produced and the corresponding assembly code files are generated.
3. Compilation
The compiled assembly code files are translated into machine instructions, and a. o file for the relocatable target program is generated, and the file is a binary file, and the byte encoding is a machine instruction.
The assembler is the instruction that transforms the assembly code into a machine executable, and almost every assembly statement corresponds to a machine instruction. So assembler assembly process is relatively simple compared to the compiler, it has no complex syntax, no semantics, and does not need to do instruction optimization, just according to the assembly instructions and machine instructions one by one translation can be.
4. Links
The linker creates a complete executable program by linking the target files (and perhaps the library files) together.
The target files generated by the assembler are not immediately executed, and there may be many unresolved issues.
For example, a function in one source file might refer to a symbol defined in another source file (such as a variable or function call), a function in a library file might be called in a program, and so on. All of these problems need to be resolved by the process of the linked program.
The main task of the linker is to connect the relevant target files to each other, that is, to connect the symbols referenced in one file to the definition of the symbol in another file, so that all these target files become a unified whole that can be loaded into the operating system.
At this point, roughly through these steps, a complete executable program has been produced.
C + + program compilation process (pre-compilation, compile-so links)