Linux下helloworld的简单编译过程

来源:互联网 发布:java jdbc连接池使用 编辑:程序博客网 时间:2024/06/06 18:34

我们在使用gcc生成可执行程序的时候,一般是命令: gcc *.c, 生成可执行文件a.out
其实过程可以分为4个步骤,分别是预处理,编译,汇编和链接。下面以最基本的helloworld来简单了解一下编译的过程。

1.预编译

预编译又称为预处理,是做些代码文本的替换工作。
处理#开头的指令,比如拷贝#include包含的文件代码,#define宏定义的替换,条件编译等,就是为编译做的预备工作的阶段,主要处理#开始的预编译指令。
gcc -E hello.c -o hello.i
生成的hello.i仍然是个文本文件

cat@lenovo:~/blog/0718$ lshello.ccat@lenovo:~/blog/0718$ gcc -E hello.c -o hello.icat@lenovo:~/blog/0718$ file hello.ihello.i: UTF-8 Unicode C program text

2.编译

把用高级程序设计语言书写的源程序,翻译成汇编语言

cat@lenovo:~/blog/0718$ lshello.c  hello.icat@lenovo:~/blog/0718$ gcc -S hello.icat@lenovo:~/blog/0718$ lshello.c  hello.i  hello.scat@lenovo:~/blog/0718$ cat hello.s    .file   "hello.c"    .section    .rodata.LC0:    .string "hello world"    .text    .globl  main    .type   main, @functionmain:.LFB0:    .cfi_startproc    pushl   %ebp    .cfi_def_cfa_offset 8    .cfi_offset 5, -8    movl    %esp, %ebp    .cfi_def_cfa_register 5    andl    $-16, %esp    subl    $16, %esp    movl    $.LC0, (%esp)    call    puts    movl    $0, %eax    leave    .cfi_restore 5    .cfi_def_cfa 4, 4    ret    .cfi_endproc.LFE0:    .size   main, .-main    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"    .section    .note.GNU-stack,"",@progbits

3.汇编

这个过程把汇编语言翻译成机器语言

cat@lenovo:~/blog/0718$ lshello.c  hello.i  hello.scat@lenovo:~/blog/0718$ gcc -c hello.scat@lenovo:~/blog/0718$ lshello.c  hello.i  hello.o  hello.scat@lenovo:~/blog/0718$ file hello.ohello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

4.链接

将生成的目标文件和其所依赖的库文件进行连接,生成一个可执行文件

cat@lenovo:~/blog/0718$ gcc hello.ocat@lenovo:~/blog/0718$ lsa.out  hello.c  hello.i  hello.o  hello.scat@lenovo:~/blog/0718$ ./a.out hello world
0 0
原创粉丝点击