gcc具有一般编译器所具有的4个编译流程:
Pre-processing -> Compiling -> Assembling -> Load and Link

我们以下面一段源代码来分析gcc编译的整个过程:

#include

int main()

{

printf("Hello, World!\n");

return 0;

}


(1)预处理(Pre-processing)
在该阶段,编译器将上述代码中的stdio.h编译进来,并且用户可以使用gcc的选项”-E”(E要大写)进行查看,该选项的作用是让gcc在预处理结束后停止编译过程。

*

注意

gcc指令的一般格式为:gcc [选项] 要编译的文件 [选项] [目标文件]
其中,目标文件可缺省,gcc默认生成可执行的文件,命为:编译文件.out

[root@localhost Gcc]# gcc -E hello.c -o hello.i

在此处,选项”-o”是指目标文件,由表3.6可知,”.i”文件为已经过预处理的C原始程序。以下列出了hello.i文件的部分内容:

# 1 "hello.c"
# 1 ""
# 1 ""
# 1 "hello.c"
# 1 "\\cygwin\\usr\\include/stdio.h" 1 3
# 29 "\\cygwin\\usr\\include/stdio.h" 3
# 1 "\\cygwin\\usr\\include/_ansi.h" 1 3
# 15 "\\cygwin\\usr\\include/_ansi.h" 3
# 1 "\\cygwin\\usr\\include/newlib.h" 1 3
# 16 "\\cygwin\\usr\\include/_ansi.h" 2 3
# 1 "\\cygwin\\usr\\include/sys/config.h" 1 3

......


typedef struct
{
int __count;
union
{
wint_t __wch;
unsigned char __wchb[4];
} __value;
} _mbstate_t;

typedef _LOCK_T _flock_t;

typedef void *_iconv_t;

......


FILE *__attribute__((__cdecl__))
funopen (const void * _cookie, int (*readfn)
(void * _cookie, char *_buf, int _n),
int (*writefn)(void * _cookie, const char *_buf, int _n),
fpos_t (*seekfn)(void * _cookie, fpos_t _off, int _whence),
int (*closefn)(void * _cookie));
# 516 "\\cygwin\\usr\\include/stdio.h" 3
extern __inline__ __attribute__ ((__always_inline__))
int __sgetc_r(struct _reent *__ptr, FILE *__p)
{
int __c = (--(__p)->_r < 0 ? __srget_r(__ptr, __p) : (int)(*(__p)->_p++));
if ((__p->_flags & 0x4000) && (__c == '\r'))
{
int __c2 = (--(__p)->_r < 0 ? __srget_r(__ptr, __p) : (int)(*(__p)->_p++));
if (__c2 == '\n')
__c = __c2;
else
ungetc(__c2, __p);
}
return __c;
}
# 598 "\\cygwin\\usr\\include/stdio.h" 3

# 2 "hello.c" 2

int main(int argc, char *argv[])
{
printf("Hello World!\n");

return 0;

}

由此可见,gcc确实进行了预处理,它把”stdio.h”的内容插入到hello.i文件中。


(2)编译阶段

接下来进行的是编译阶段,在这个阶段中,Gcc首先要检查代码的规范性、是否有语法错误等,以确定代码的实际要做的工作,在检查无误后,Gcc把代码翻译成汇编语言。用户可以使用”-S”选项来进行查看,该选项只进行编译而不进行汇编,生成汇编代码。

[root@localhost Gcc]# gcc -S hello.i -o hello.s

以下列出了hello.s的部分内容,可见gcc已经将其转化为汇编了,感兴趣的读者可以分析一下这一行简单的C语言小程序是如何用汇编代码实现的:

.file "hello.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "Hello World!\12\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
ret
.def _printf; .scl 3; .type 32; .endef


(3)汇编阶段

汇编阶段是把编译阶段生成的”.s”文件转成目标文件,读者在此可使用选项”-c”就可看到汇编代码已转化为”.o”的二进制目标代码了。如下所示:

[root@localhost Gcc]# gcc -c hello.s -o hello.o

(4)链接阶段

在成功编译之后,就进入了链接阶段。在这里涉及到一个重要的概念:函数库。

读者可以重新查看这个小程序,在这个程序中并没有定义”printf”的函数实现,且在预编译中包含进的”stdio.h”中也只有该函数的声明,而没有定义函数的实现,那么,是在哪里实现”printf”函数的呢?最后的答案是:系统把这些函数实现都被做到名为libc.so.6的库文件中去了,在没有特别指定时,gcc会到系统默认的搜索路径”/usr/lib”下进行查找,也就是链接到libc.so.6库函数中去,这样就能实现函数”printf”了,而这也就是链接的作用。

函数库一般分为静态库和动态库两种。静态库是指编译链接时,把库文件的代码全部加入到可执行文件中,因此生成的文件比较大,但在运行时也就不再需要库文件了。其后缀名一般为”.a”。动态库与之相反,在编译链接时并没有把库文件的代码加入到可执行文件中,而是在程序执行时由运行时链接文件加载库,这样可以节省系统的开销。动态库一般后缀名为”.so”,如前面所述的libc.so.6就是动态库。gcc在编译时默认使用动态库。

完成了链接之后,gcc就可以生成可执行文件了:

[root@localhost Gcc]# gcc hello.o -o hello

运行该可执行文件,出现正确的结果如下:

[root@localhost Gcc]# ./hello
Hello, World!

preload preload preload