GCC编译过程分解
以helloworld为例分析gcc编译过程:
#include
int main()
{
printf(“Hello World\n”);
return 0;
}
GCC编译过程可以被细分为四个阶段:预处理 编译 汇编 链接
1.预处理
由于在helloworld.c中使用了头文件stdio.h,所以GCC在编译时首先要把头文件stdio.h中的内容加载到helloworld.c中的首部
输入命令“gcc -E helloworld.c -o helloworld.i”
其中参数E告诉gcc命令只进行预编译,不做其他处理;其中参数o用来指明输出的文件名为helloworld.i
命令运行完毕后就会产生一个名为helloworld.i的文件,编译如下:
[root@localhost 0707]# gcc -E helloworld.c -o helloworld.i
[root@localhost 0707]# ls
helloworld.c helloworld.i
helloworld.i文件的代码有一百多行,如下是helloworld.i的最后部分代码
[root@localhost 0707]# ls
helloworld.c helloworld.i
helloworld.i文件的代码有一百多行,如下是helloworld.i的最后部分代码
extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__)) ;
# 774 "/usr/include/stdio.h" 3 4
extern FILE *popen (__const char *__command, __const char *__modes) ;
extern int pclose (FILE *__stream);
extern char *ctermid (char *__s) __attribute__ ((__nothrow__));
# 814 "/usr/include/stdio.h" 3 4
extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__));
extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__));
# 844 "/usr/include/stdio.h" 3 4
# 2 "helloworld.c" 2
int main()
{
printf("Hello World\n");
# 774 "/usr/include/stdio.h" 3 4
extern FILE *popen (__const char *__command, __const char *__modes) ;
extern int pclose (FILE *__stream);
extern char *ctermid (char *__s) __attribute__ ((__nothrow__));
# 814 "/usr/include/stdio.h" 3 4
extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__));
extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__)) ;
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__));
# 844 "/usr/include/stdio.h" 3 4
# 2 "helloworld.c" 2
int main()
{
printf("Hello World\n");
return 0;
}
在这个程序中预编译中对头文件进行了加载,除了对头文件加载预编译中还对什么进行处理?
预编译中除了对头文件的加载之外,还完成宏替换和条件编译
2.编译
编译阶段主要是来进行语法检查和语法分析
输入命令“gcc -S helloworld.i -o helloworld.s ”
其中,参数S告诉gcc命令只进行编译,不进行其他处理。命令运行完毕后就会产生一个名为helloworld.s的汇编文件,如下所示:
[root@localhost 0707]# gcc -S helloworld.i -o helloworld.s
[root@localhost 0707]# ls
helloworld.c helloworld.i helloworld.s
[root@localhost 0707]# ls
helloworld.c helloworld.i helloworld.s
3.汇编
汇编阶段的任务是把在第二阶段生成的汇编程序翻译成CPU能够识别的二进制文件,生成的这个文件又称为目标文件
输入命令“gcc -c helloworld.s -o helloworld.o”
其中,参数c告诉gcc命令只进行汇编,不做其他处理。命令运行完毕后会产生一个名为helloworld.o的目标文件
[root@localhost 0707]# gcc -c helloworld.s -o helloworld.o
[root@localhost 0707]# ls
helloworld.c helloworld.i helloworld.o helloworld.s
[root@localhost 0707]# ls
helloworld.c helloworld.i helloworld.o helloworld.s
4.链接
目标文件虽然已经可以被 CPU直接识别,但是单个目标文件一般是无法运行的。由于一个程序往往是有多个源文件组成的,每个源文件只对应一个目标文件。也许有人会问,helloworld程序不就只有一个源文件helloworld.c吗,为什么也不能直接运行呢?原因是helloworld.c使用了stdio.h对应的函数库,所以必须要把目标文件与函数库文件链接在一起才能运行。
链接就是把程序中所有的目标文件和所需的库文件都链接在一起,最终生成一个可以直接运行的文件,成为可执行文件。
在链接阶段根据不同的链接库的方式可以分为静态库和动态库,关于静态库与动态库的生成与区别,我会在下章中详细说明。