gcc编译与程序的4个执行阶段解读

在使用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. }  

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

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

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

   A.预处理(Pr-Processing)

 

Java代码  收藏代码
  1. 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. ....  

 B.编译(Compiling)

Java代码  收藏代码
  1. [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  

 

  C.汇编(Assembling)

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

Java代码  收藏代码
  1. [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  

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

posted @ 2013-10-01 10:42  ps龙之吻  阅读(331)  评论(0编辑  收藏  举报