文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[转]GCC编译过程的4个阶段

[转]GCC编译过程的4个阶段

时间:2010-09-26  来源:ittripmaster

GCC编译过程的4个阶段

文章分类:C++编程

  在使用GCC编译程序时,其编译过程可以分为4个阶段:预处理(Pre-Processing) -> 编译(Compling) -> 汇编(Assembling) -> 连接(Linking) .

  程序员完全可以根据自己的需要让gcc在编译的任何一个阶段结束,检查活使用编译起在该阶段输出的信息,或者对其生成的二进制文件进行控制。

   我想通过一个Hello World程序对GCC的编译过程有进一步的认识。

C代码
  1. [Xredman@localhost demo]$ cat test.c   
  2. #include <stdio.h>   
  3.   
  4. int main(void)   
  5. {   
  6.         printf("Hello World\n");   
  7.         return 0;   
  8. }  
[Xredman@localhost demo]$ cat test.c
#include <stdio.h>

int main(void)
{
        printf("Hello World\n");
        return 0;
}

  首先来个一步到位编译成可执行文件

C代码
  1. [Xredman@localhost demo]$ gcc -Wall test.c -o test.o   
  2. [Xredman@localhost demo]$ ./test.o    
  3. Hello World  
[Xredman@localhost demo]$ gcc -Wall test.c -o test.o
[Xredman@localhost demo]$ ./test.o 
Hello World

   OK,这是我日常生活中所用的,以下将分步骤演示

   A.预处理(Pr-Processing)

 

Java代码
  1. gcc -E test.c -o test.i  
gcc -E test.c -o test.i

   打开test.i文件,吓我嘛,编译器原来为我们做了这么多事,辛苦了。这里主要负责展开在源文件中定义的宏,并向其中插入#include语句所包含的内容

C代码
  1. # 1 "test.c"   
  2. # 1 "<built-in>"   
  3. # 1 "<command line>"   
  4. # 1 "test.c"   
  5. # 1 "/usr/include/stdio.h" 1 3 4   
  6. # 28 "/usr/include/stdio.h" 3 4   
  7. # 1 "/usr/include/features.h" 1 3 4   
  8. # 329 "/usr/include/features.h" 3 4   
  9. # 1 "/usr/include/sys/cdefs.h" 1 3 4   
  10. # 313 "/usr/include/sys/cdefs.h" 3 4   
  11. # 1 "/usr/include/bits/wordsize.h" 1 3 4   
  12. # 314 "/usr/include/sys/cdefs.h" 2 3 4   
  13. # 330 "/usr/include/features.h" 2 3 4   
  14. # 352 "/usr/include/features.h" 3 4   
  15. # 1 "/usr/include/gnu/stubs.h" 1 3 4   
  16.   
  17.   
  18.   
  19. # 1 "/usr/include/bits/wordsize.h" 1 3 4   
  20. # 5 "/usr/include/gnu/stubs.h" 2 3 4   
  21.   
  22.   
  23. # 1 "/usr/include/gnu/stubs-32.h" 1 3 4   
  24. ....  
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "test.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 28 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 329 "/usr/include/features.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 313 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 314 "/usr/include/sys/cdefs.h" 2 3 4
# 330 "/usr/include/features.h" 2 3 4
# 352 "/usr/include/features.h" 3 4
# 1 "/usr/include/gnu/stubs.h" 1 3 4



# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 5 "/usr/include/gnu/stubs.h" 2 3 4


# 1 "/usr/include/gnu/stubs-32.h" 1 3 4
....

 B.编译(Compiling)

Java代码
  1. [Xredman@localhost demo]$ gcc -S test.i -o test.s  
[Xredman@localhost demo]$ gcc -S test.i -o test.s

   编译之后应该就是汇编程序了吧,查看内容如下

Assemble代码
  1. [Xredman@localhost demo]$ cat test.s   
  2.         .file   "test.c"  
  3.         .section        .rodata   
  4. .LC0:   
  5.         .string "Hello World"  
  6.         .text   
  7. .globl main   
  8.         .type   main, @function   
  9. main:   
  10.         leal    4(%esp), %ecx   
  11.         andl    $-16, %esp   
  12.         pushl   -4(%ecx)   
  13.         pushl   %ebp   
  14.         movl    %esp, %ebp   
  15.         pushl   %ecx   
  16.         subl    $4, %esp   
  17.         movl    $.LC0, (%esp)   
  18.         call    puts   
  19.         movl    $0, %eax   
  20.         addl    $4, %esp   
  21.         popl    %ecx   
  22.         popl    %ebp   
  23.         leal    -4(%ecx), %esp   
  24.         ret   
  25.         .size   main, .-main   
  26.         .ident  "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-48)"  
  27.         .section        .note.GNU-stack,"",@progbits  
[Xredman@localhost demo]$ cat test.s
        .file   "test.c"
        .section        .rodata
.LC0:
        .string "Hello World"
        .text
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $4, %esp
        movl    $.LC0, (%esp)
        call    puts
        movl    $0, %eax
        addl    $4, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-48)"
        .section        .note.GNU-stack,"",@progbits
 

  C.汇编(Assembling)

    汇编这一步是将汇编程序翻译成机器码,这也就决定了生成的程序能运行在哪种平台下,传说中GCC将调用gas汇编器

Java代码
  1. [Xredman@localhost demo]$ gcc -c test.s -o test.o  
[Xredman@localhost demo]$ gcc -c test.s -o test.o

   D.连接(Linking)

   这是最后一步,将生成的目标文件和其所依赖的库文件进行连接,生成一个可执行文件。

Java代码
  1. [Xredman@localhost demo]$ gcc test.o -o test   
  2. [Xredman@localhost demo]$ ./test   
  3. Hello World  
[Xredman@localhost demo]$ gcc test.o -o test
[Xredman@localhost demo]$ ./test
Hello World

 OK,练习完毕,我已经知道GCC的基本工作过程了。

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载