编译过程:
- 预处理:预处理器将对源文件中的宏进行展开。
- 编译:编译器gcc将c文件编译成汇编文件。
- 汇编:汇编器as将汇编文件编译成机器码。
- 链接:将目标文件和外部符号进行连接,得到一个可执行二进制文件。
以一个很简单的01_test.c来探讨这个过程。
#include <stdio.h>
#define NUMBER (1 + 2)
int main(void)
{
int x = NUMBER;
printf("x:%d\n",x);
return 0;
}
- 预处理
[csy@local compile_test]$ gcc -E -o 01_test.i 01_test.c
用cat查看01_test.i的内容如下:
int main(void)
{
int x = (1 + 2);
printf("x:%d\n",x);
return 0;
}
可以看到,文件中宏定义NUMBER出现的位置被(1+2)替换掉了,其它的内容保持不变。
2. 编译成汇编代码
gcc -S -o 01_test.s 01_test.i
通过cat test.s查看01_test.s的内容为汇编代码。
[csy@local compile_test]$ cat 01_test.s
.file "01_test.c"
.section .rodata
.LC0:
.string "x:%d\n"
.text
.globl main
.type main, @function
main:
pushl %