Take gcc on Linux system as an example to decrypt the whole process behind C language compilation!

In this Internet age, many people have chosen to major in computer science, and as long as they are small partners in computer science, they may all learn C language, but are you all aware of the complete process of C language compilation? Today I will take everyone to do it together Let's decrypt it.

C language is a high-level language relative to assembly language. To run on the system, it needs to be converted into executable code that can be read by the machine through a compiler.

Take gcc on the Linux system as an example. Usually we compile a source file with the following command:

$gcc hello.c –o hello

 

After the compilation is successful, the hello program will be generated in the directory. Run it directly to see the result.

$. / hello
Hello World!

But how is the hello program generated? There are actually several steps in the middle. Recompile with the following command, you can see all the intermediate files.

$gcc -save-temps hello.c –o hello
$ls
hello hello.c hello.i hello.o hello.s

 

The compilation process of the C compiler is mainly divided into four steps:

(1) Pretreatment

(2) Compile

(3) Assembly

(4) Connection

 

 

1) Pre-prosssing

The preprocessing generates an intermediate file of hello.i, which mainly completes the following steps:

✿ Remove all comments

✿ Expand all macro definitions (that is, do character substitution)

✿ Insert the content of #include file

✿ Handle all conditional compilation

 

The content of the hello.i file is as follows (the file is larger and only the bottom piece is shown):

 

 

It can be found that all the comments in the source code have been deleted, and the content of the stdio.h header file has been inserted.

 

2) Compiling

Compilation The hello.i file is compiled to generate an intermediate file hello.s. When you open it, you can see that it is full of assembly language, so the function of compilation is to convert the source code into assembly language.

 

 

 

3) Assembly

The assembler assembles hello.s into a hello.o file. hello.o is a binary file, which contains code that can be executed by the machine.

 

 

 

4) Linking

As the name suggests, the connection plays a role in connection. Although hello.o is already a binary file, the printf function used in it needs to call other libraries. The linker binds our binary file to other libraries. You can see that the hello file generated after the connection is much larger than hello.o.

 

 

At this point, the complete compilation process of C is over. The example in this article uses the Linux operating system and the compiler uses gcc, but in other operating systems, such as Unix, Windows, or other compilers, the principles are the same. Interested students can go to learn the principles of compilation and have a deeper understanding of compilation.

Guess you like

Origin blog.csdn.net/u010608296/article/details/113108514