Linux下GCC编程四个过程(2)

来源:互联网 发布:淘宝该用户已被冻结 编辑:程序博客网 时间:2024/04/29 16:28

2)编译阶段(Compiling)

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

选项 -S

用法:[root]# gcc –S hello.i –o hello.s

作用:将预处理输出文件hello.i汇编成hello.s文件。

[root@richard hello-gcc]# ls

hello.c  hello.i  hello.s

如下为hello.s汇编代码

[root@richard hello-gcc]# vi hello.s
.file   "hello.c"
.section    .rodata
.LC0:
.string "hello world!/r/n"
.text
.globl main
.type   main,@function
main:
pushl   %ebp
movl    %esp, %ebp
subl    $8, %esp
andl    $-16, %esp
movl    $0, %eax
subl    %eax, %esp
subl    $12, %esp
pushl   $.LC0
call    printf
addl    $16, %esp
movl    $0, %eax
leave
ret
.Lfe1:
.size   main,.Lfe1-main
.ident  "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"

3)汇编阶段(Assembling)

汇编阶段是把编译阶段生成的”.s”文件转成二进制目标代码.

选项 -c

用法:[root]# gcc –c hello.s –o hello.o

作用:将汇编输出文件test.s编译输出test.o文件。

[root]# gcc -c hello.s -o hello.o

[root]# ls

hello.c  hello.i  hello.o  hello.s