After reading this article, I finally understand what's going on with the compilation

After reading this article , I finally understand what's going on with the compilation.

1

For the same statement, there are the following three: high-level language, low-level language, machine language representation

  • C language 
    a=b+1;

  • Assembly language 
    mov -0xc(%ebp),%eax
    add $0x1,%eax
    mov %eax,-0x8(%ebp)

  • ML 
    8b 45 f4
    83 c0 01
    89 45 f8

We all know that machines can only do numerical calculations, and the language that allows machines to calculate and numbers is machine language, and all other computer languages ​​are non-machine languages. Such high-level language relative to machine language requires a conversion from high-level, machine-incomprehensible, to machine-understandable machine language. Such a conversion process is called 编译(Compile), 由编译器(Compiler)to complete.

The process of 汇编器(Assembler)converting performed by .

The conversion of C and assembly language to machine language is done by the compiler.

2

Here, C is cross-platform, or it can be said to be platform-independent. There are two versions of the platform here, one refers to the computer 体系(Architecture), the other refers to 操作系统(Operate System), or a combination of the two. Different platforms require different instruction sets to execute machine language. The cross-platform nature of C means that you only need to write a C program code that does not need to be modified, and it can run on computers with different systems and different operating systems. This is all thanks to the compiler, which translates the C program into a machine language suitable for the current computer system.

Let's talk about the whole process of compiling C language into machine language:

First, we write a C program code, name the code hello.c, this code file, we call it 源代码(Srouce Code).

然后我们运行编译器,对该源代码文件进行编译,在整个编译的过程中,编译器并不会执行该源代码,只是生成一份新的机器语言代码文件,如hello.out。这份新生成的代码文件称为目标代码(Object Code)可执行代码(Executable)

3

对于编译过程,里面还涉及到具体的一些可以说的细节步骤。

Linux下,使用gcc编译器:

预编译hello.c文件:

gcc -E -o hello.i hello.c

执行成功后就会生成一个新的hello.i的文件,可以用编辑器(Vim)查看它的内容,这个文件就是经过预编译后的内容。

预编译又称为预处理,是做些代码文本的替换工作。预编译可以处理#开头的指令,比如拷贝#include包含的文件代码,#define的宏定义的替换,条件编译等。

纯粹的进行编译:

gcc -S -o hello.s hello.i

把.i文件写为hello.c也行,就是跳过手动预编译,直接完成预编译和编译两个过程。这时会得到一个hello.s文件,打开看一下,里面是编译好的使用于当前体系结构的汇编代码。

把汇编代码进行汇编可执行:

gcc -c -o hello.o hello.s

把.s文件换成.c也行,就是自动完成预编译、编译和汇编三个过程。现在得到一个hello.o文件,这是一个二进制文件,但不是最后的可执行二进制文件,因为它还缺少最后一步连接处理。

最后对.o文件进行连接,我们这里就一个.o文件所以简单,经常是需要有多个.o文件需要连接。连接执行:

gcc -o hello hello.o

如果把最后的.o文件写成.c,那就和最开始我们用hello.c编译时示范的那样了。实际上那样是完成了预编译、编译、汇编和连接一连串的过程。

想了解更多gcc的只是可以到GNU的网站上去看看。

BTW,gdb是常用的调式软件。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325890939&siteId=291194637