C/C++ programming notes: the working principle of the C language compiler

2020/12/0723:40:10 technology 1729

takes the gcc compiler as an example. The compilation actually goes through four stages:

preprocessing--->compiling--->assembly--->link

C/C++ programming notes: the working principle of the C language compiler - DayDayNews

1. Preprocessing stage: The compiler uses the cpp file as a unit, First read this cpp file, and find that the first sentence and the second sentence contain a header file, it will look for these two header files in all search paths, and after finding it, it will go to the corresponding header file to process macros, variables, and functions. Declarations, nested header files, etc.

detects dependencies, performs macro replacements to see if there are duplicate definitions and declarations, and finally scans all the stuff in those files into the current cpp file to form an intermediate "cpp file". In this step,

is equivalent to scanning the test variable in that header file into an intermediate cpp file, then the test variable becomes a global variable in this file. There are some function declarations in the stdio.h header file. At this time, these function declarations are also scanned into the intermediate cpp file (only the function declarations are scanned, but they are not implemented).

C/C++ programming notes: the working principle of the C language compiler - DayDayNews

2. Compilation stage: At this time, space is allocated for all variables and function parameters of the intermediate cpp file (in principle, only the declaration of functions and variables in the .h file can be seen here, which are variables and function parameters, etc. Allocate space), compile each function into a binary code, and generate an object file according to a specific object file format.

performs symbolic description of each global variable and function in the target file of this format (the compiler maintains a symbol description table), and organizes these binary codes into a target file according to a certain standard.

C/C++ programming notes: the working principle of the C language compiler - DayDayNews

At this time, each cpp file is compiled into an object file by the compiler, and each object file has a symbol table. This symbol table records which variables, which functions, and functions are used in the cpp file. There are several parameters for what type of parameter.

also opens up memory space for variables and function parameters. So here the compiler records all the things used by your cpp, if there are repeated definitions or undefined variables, functions, etc., the compiler will know all at once.

3. Linking stage: Connect each target file generated in the previous step to generate the final executable file according to some parameters. The main work is to relocate the functions and variables of each target file, which is equivalent to relocating the functions and variables of each target file. Binary codes are combined into one file according to certain specifications.

C/C++ programming notes: the working principle of the C language compiler - DayDayNews

technology Category Latest News