C/C++: Program compilation process

1. Preprocessing

Preprocessing is equivalent to assembling a new C/C++ program according to the preprocessing directives. After preprocessing, an output file with no macro definitions, no conditional compilation instructions, and no special symbols will be generated. The meaning of this file is the same as the original file, but the content is different.

Read the C/C++ source program and process the pseudo-instructions (instructions beginning with # )

① Delete all "#define" and expand all macro definitions

Process all conditional compilation instructions, such as: "#if" , "#ifdef" , "#elif" , "#else" , "endif" , etc. The introduction of these directives allows the programmer to decide which code the compiler processes by defining different macros. The precompiler will filter out unnecessary code based on the relevant files.

Process the "#include" precompiled instruction, and insert the included file into the location of the precompiled instruction.

(Note: this process may be recursive, which means that the included file may also contain other files)

delete all comments

Add line number and filename identifiers.

In order to facilitate the compiler to generate line number information for debugging and to display line numbers for compilation errors or warnings generated during compilation

All #pragma compiler directives are preserved

2. Compile

After performing a series of lexical analysis, syntax analysis, semantic analysis and optimization on the preprocessed files, the corresponding assembly code files are generated.

3. Compilation

The compiled assembly code file is translated into machine instructions, and the .o file of the relocatable object program is generated . The file is a binary file, and the byte encoding is the machine instruction.

The assembler converts assembly code into instructions that can be executed by the machine, and almost every assembly statement corresponds to a machine instruction. Therefore, the assembly process of the assembler is relatively simple compared to the compiler. It has no complex syntax, no semantics, and no instruction optimization. It is only necessary to translate one by one according to the comparison table of assembly instructions and machine instructions.

4. Links

The object files (and perhaps library files) are linked together by the linker to generate a complete executable program.

Object files generated by the assembler cannot be executed immediately, and there may be many unresolved problems.

For example, a function in a source file may reference a symbol (such as a variable or function call, etc.) defined in another source file; a function in a library file may be called in a program, and so on. All of these problems need to be processed by the linker to be solved.

The main job of the linker is to connect the related object files with each other, that is, to connect the symbols referenced in one file with the definitions of the symbols in another file, so that all these object files become one that can be used by the operating system. Load the unified whole of execution.

So far, after roughly these steps, a complete executable program is generated.

 

(Internal linking and external linking occur during the linking process of the program)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648185&siteId=291194637