文章列表

源码编译成为可运行的二进制文件,有好几个步骤,本文用一个最简单的例子,详细解释编译器的每一步都在干什么。

编译型语言,Linux+C语言,但是可以做为扩展到编译型语言的

Life of a binary | Thoughts of a programmer


文章列表_第1张图片

1.First step is the Preprocessing step which is done by the Preprocessor. Job of the Preprocessor is to handle all the preprocessor directives(预处理指令) present in your code. These directives start with#. But before it processes them, it first removes all the comments from the code as comments are there only for the human readability(预处理之前会移除所有的注释). Then it finds all the#commands, and does what the commands says.

In the code above, we have just used#includedirective, which simply says to the Preprocesssor to copy thestdio.hfile and paste it into this file at the current location.(在一个简单的例子中可以只使用#include,让预处理器copystdio.h文件进来)

2。拿到上一步的输出之后,通过词法分析器(lexical analyser)分辨出like ‘int’, ‘return’, ‘void’, ‘0’ and so on.通过句法分析器(syntax analyser)看语法是否有误,之后semantic analyser,看type checking and variables are declared before their first usage, etc.之后就转换为特定平台的汇编语言,但是也可以手动设置转为别的平台的汇编语言

3.得到一个.o的目标文件(不同平台得到的.o文件不同),这个.o文件中包含了机器码,当然也包含了其他许多的信息,详细请看作者原文

What we have right now is our program in the assembly language, but it is still in the language which is not understood by the processors. We have to convert the assembly language to the machine language, and that work is done by the Assembler. Assembler takes your assembly file and produces an object file which is a binary file containing the machine instructions for your program.

4.至此所有的工作都是建立在基于一个源文件的基础上,但在实际的开发中几乎不会遇见只有一个源文件的情况,因此当有许多个源文件的时候,这些源文件都会经过以上相同的步骤然后产生许多独立的.o文件。

链接器创建一个单个对象文件,将每个单个对象文件中的所有部分组合到相应的部分中,并重新定位所有可以解析的符号。

例如调用了print函数就要链接

At this point, there is one thing you should know. The functions and data you use from other libraries, can be statically linked or dynamically linked. Static linking means that the functions and data from those libraries would be copied and pasted into your executable. Whereas, if you do dynamic linking, then those functions and data are not copied into your executable, thus reducing your final executable size.

5.将可执行文件装载进入内存执行

你可能感兴趣的:(文章列表)