Most of our programmers may have learned from C language, and have written tens of thousands, hundreds of thousands of lines, or even millions of lines of code. But do you all know the complete process of C language compilation? If not, I will take you to do a decryption today.
C language is a high-level language relative to assembly language. In order to run on the system, we need to convert it into executable code that the machine can read.
Taking GCC on Linux as an example, we usually compile a source file with the following command:
$gcc hello.c –o hello
After the successful compilation, the directory will generate the Hello program, run it directly to see the results.
But how does Hello program generate? Actually, there are several steps in the middle. Recompile it with the following command, and you can see all the intermediate files.
$gcc -save-temps hello.c –o hello
hello hello.c hello.i hello.o hello.s
The compiling process of C compiler is divided into four steps
1) Pre Prossing
Preprocessing generates the intermediate file of Hello. I, which mainly completes the following steps:
Remove all comments
Expand all macro definitions (i.e. character substitution)
Insert the contents of the include file
Handle all conditional compilation
The contents of the hello. I file are as follows (the file is large and only the bottom part is shown)
You can find that all comments in the source code have been removed and the contents of the stdio. H header file have been inserted.
The compiler compiles the hello. I file into an intermediate file hello. S. when you open it, you can see that there are assembly languages in it. Therefore, the function of compilation is to convert the source code into assembly language.
Assemble the hello. Assembler. Hello. O is a binary file with code that the machine can execute.
As the name suggests, connection plays a role in connection. Although hello. O is already a binary file, the functions used in it, such as printf, need to call other libraries. The connector makes a binding between our binaries and other libraries. You can see that the generated Hello file is much larger than hello. O.
At this point, the complete compilation process of C is finished. The examples in this paper use Linux operating system, and compiler uses GCC. However, in other operating systems, such as UNIX, windows, or other compilers, the principle is the same. Interested students can learn the compilation principle and have a deeper understanding of compilation.