Reinterpretation of the four stages of program compilation

The 4 processes of program compilation (recording and posting, the first blog of C++ trainees who have studied for two and a half months, if there are any errors, please raise them)

When using the compiler to compile a section of program, when we finish editing, we only need to click the compile button, and the compiler will return the compilation result of this section of program. If there is an error, it will return the wrong place and error message, which is convenient for us. Modify the program at the location so that the program can be compiled. But have we ever entangled what kind of process the compiler is when compiling. We only know this section of the program. The function is divided into four stages when compiling:
1. Pre-compilation;
2. Compilation;
3. Assembly;
4. Link;
Insert picture description here

		先来看一段代码:
#include<stdio.h>
int main()
{
    
    
	printf("hello c world!");
	return 0;
}

This is the first piece of code written by most people when they are learning IT. Let us use this piece of code to familiarize ourselves with the whole process of the compiler compiling the code!

1. Pre-compilation

Every time we write C language, we will write #include<stdio.h> and so on a piece of code beginning with #include<>, and the purpose of pre-compilation is to interpret similar codes and process such header files, similar For other macro definitions. At the same time, we are writing code comments, through pre-compilation, you can deal with these things that are not important for code compilation. After the pre-compilation is completed, the source file of hello.i will be generated .
Pre-compiled rules:

  1. Delete all "#define" and expand the macro definition;
  2. Process all conditional precompiled instructions: “#if” “#ifdef” “#elif” “#else” “#endif” and so on;
  3. Delete all notes;
  4. Add line number and file name identification;
  5. Process all #include header file references;
  6. Keep all #pragma compiler directives;

2. Compile

The compilation process is mainly to perform a series of lexical analysis, grammatical analysis, semantic analysis on the preprocessed file and generate assembly file code (.O). Perform grammatical analysis and judge whether the grammar is right or wrong, and give hints. The fate of whether a piece of code can be compiled is almost entirely controlled at this stage. The function compilation process is carried out from top to bottom, instead of starting from the main() function when the function is executed, so people who have just entered the pit often make some mistakes. Come, see the code:

#include<stdio.h>
int add(int x,int y)
{
    
    	
	return max=x+y;
}

int main()
{
    
    
	int max,c;
	int a=10;
	int b=20;
	printf("%d \",add(a,b));
	return 0;
}	

Come and click what is the result of compiling Kangkang, and the result is given:
The compilation result of the above program
Since the compiler compiles from top to bottom when compiling, the compiler checks that max was in the previous compilation process when running the add() function. Encountered, so I directly killed and sentenced this procedure to death. Even in the most important main program, max has been defined, but because the visibility of max is only in the main function and does not extend to the add() function, the program fails to compile. (Have you noticed the introduction of the concept of visibility, come back later)

3. Compilation

For this process, when the most powerful function of the compiler is manifested, it converts the language that our humans have reached the standard into assembly language that can be read by the machine, and realizes the real human-computer interaction. It also completes the communication translation between two completely different individuals, making the computer a tool in the hands of the developer.

4. Link

For a huge project file, the realization of the function is done through multiple .cpp files or .c files. Different functions are used to complete different functions, and then the various modules are assembled through the linking process to complete the last step. Ever since, the hello.exe file is generated. The files that can be run can be used, and the results of the code can be executed normally after the developer clicks to run.

Guess you like

Origin blog.csdn.net/ALITAAAA/article/details/102961643