我现在一般都是用gcc,所以自然以GCC编译hellworld为例,简单总结如下。
hello.c源代码如下:
点击(此处)折叠或打开
- #include <stdio.h>
- int main()
- {
- printf(“Hello, world.\n”);
- return 0;
- }
通常我们使用gcc来生成可执行程序,命令为:gcc hello.c,默认生成可执行文件a.out
其实编译(包括链接)的命令:gcc hello.c 可分解为如下4个大的步骤:
- 预处理(Preprocessing)
- 编译(Compilation)
- 汇编(Assembly)
- 链接(Linking)
1. 预处理(Preproceessing)
预处理的过程主要处理包括以下过程:
将所有的#define删除,并且展开所有的宏定义
处理所有的条件预编译指令,比如#if #ifdef #elif #else #endif等
处理#include 预编译指令,将被包含的文件插入到该预编译指令的位置。
删除所有注释 “//”和”/* */”.
添加行号和文件标识,以便编译时产生调试用的行号及编译错误警告行号。
保留所有的#pragma编译器指令,因为编译器需要使用它们
通常使用以下命令来进行预处理:
gcc -E hello.c -o hello.i
参数-E表示只进行预处理 或者也可以使用以下指令完成预处理过程
cpp hello.c > hello.i /* cpp – The C Preprocessor */
直接cat hello.i 你就可以看到预处理后的代码
2. 编译(Compilation)
编译过程就是把预处理完的文件进行一系列的词法分析,语法分析,语义分析及优化后生成相应的汇编代码。
$gcc –S hello.i –o hello.s
或者
$ /usr/lib/gcc/i486-linux-gnu/4.4/cc1 hello.c
注:现在版本的GCC把预处理和编译两个步骤合成一个步骤,用cc1工具来完成。gcc其实是后台程序的一些包装,根据不同参数去调用其他的实际处理程序,比如:预编译编译程序cc1、汇编器as、连接器ld
可以看到编译后的汇编代码(hello.s)如下:
点击(此处)折叠或打开
- .file "hello.c"
- .section .rodata
- .LC0:
- .string "Hello, world."
- .text
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- andl $-16, %esp
- subl $16, %esp
- movl $.LC0, (%esp)
- call puts
- movl $0, %eax
- leave
- ret
- .size main, .-main
- .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
- .section .note.GNU-stack,"",@progbits