Most of our programmers are probably from C Language learning , Tens of thousands of lines , Hundreds of thousands of lines , Even millions of lines of code , But do you all know C The whole process of language compilation , If not clear , I'll take you all together today to make a decryption .

 

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

 

with Linux On system gcc take as an example , Usually we compile a source file with the following command :
$gcc hello.c –o hello

 

  After compilation , It will be generated in the directory hello This program , Run it directly to see the results .

$./hello

Hello World!

 

but hello How is this program generated , In fact, there are several steps in the process . 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

 

C The compiling process of compiler is divided into four steps :

(1) Pretreatment

(2) compile

(3) assembly

(4) connect

 1) Pretreatment Pre-prosssing

  Preprocessing generated hello.i Intermediate file of , Mainly completed the following steps :

*
Remove all comments

*
Expand all macro definitions ( That is, character substitution )

*
insert #include Contents of the document

*
Handle all conditional compilation

hello.i The contents of the document are as follows ( Large file size , Only the bottom part is shown ):

  

You can find that all comments in the source code have been deleted , And inserted stdio.h Content of header file .

  

2) compile Compiling

  Compilation will hello.i File compilation generates an intermediate file hello.s, You can see that there are assembly languages inside , So the purpose of compilation is to convert the source code into assembly language .

 

 3) assembly Assembly

  Assembler will hello.s Compiled into hello.o file .hello.o It's binary , It's all machine executable code .

  

4) connect Linking

  Connection, as the name suggests, plays a connecting role , although hello.o It's already binary , But there's something inside, like printf
Function needs to call another library . Connector makes a binding between our binary and other libraries . You can see the generated hello Files are better than hello.o Much bigger .

  

Come here C The complete compilation process of is over , The examples in this article are Linux operating system , The compiler uses gcc, But in other operating systems , such as
Unix,Windows, Or use another compiler , The principle is the same , Interested students can learn the principles of compilation , A deeper understanding of compilation .

 

 

Technology
©2019-2020 Toolsou All rights reserved,
Codeup——601 | problem A: task scheduling Image explanation of over fitting and under fitting CSS architecture design Message quality platform series | Full link troubleshooting How to use it quickly html and css Write static page 2021 year 1 Monthly programmer salary statistics , average 14915 element Codeup——604 | problem B: Full Permutation NOI2019 travels intermediate JAVA Data structure knowledge that programmers should master Random forest R Language implementation