代码编译过程

来源:互联网 发布:淘宝网卖家信用等级 编辑:程序博客网 时间:2024/05/16 20:58

一个.c文件到可执行文件的过程有:

1. pre-process 预编译

2. compile 编译

3. assembly汇编

4. link连接


一个hello world程序

#include <stdio.h>int main(int argc, char **argv){printf("hello world\n");return 0;}


1. 预编译,使用gcc 的-E选项

     gcc -E hello.c

把各种头文件加进来

     gcc -E hello.c > hello.i

hello.i中的内容见附件


2. 编译

gcc -S hello.c

.file"hello.c".section.rodata.LC0:.string"hello world".text.globlmain.typemain, @functionmain:.LFB0:.cfi_startprocpushq%rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq%rsp, %rbp.cfi_def_cfa_register 6subq$16, %rspmovl%edi, -4(%rbp)movq%rsi, -16(%rbp)movl$.LC0, %edicallputsmovl$0, %eaxleave.cfi_def_cfa 7, 8ret


3. 汇编,生产.o文件

gcc -c hello.c -o hello.o


4. 连接, ld

不能直接

ld  hello.o

因为还有系统的.o文件,没有一起连接



以上只是阐述编译的简单过程,和生成的一些文件


本来应该是一步步,把.c生成.i,.i文件生成.s依次最后生成可执行文件

无奈才疏学浅,还是要学习啊



想再细致的了解gcc的编译过程可以看这位大神的博客:

http://www.cnblogs.com/hnrainll/archive/2012/07/05/2578277.html

0 0