Linux下helloworld的简单编译过程
2015-07-20 11:52
447 查看
我们在使用gcc生成可执行程序的时候,一般是命令: gcc *.c, 生成可执行文件a.out
其实过程可以分为4个步骤,分别是预处理,编译,汇编和链接。下面以最基本的helloworld来简单了解一下编译的过程。
处理#开头的指令,比如拷贝#include包含的文件代码,#define宏定义的替换,条件编译等,就是为编译做的预备工作的阶段,主要处理#开始的预编译指令。
gcc -E hello.c -o hello.i
生成的hello.i仍然是个文本文件
其实过程可以分为4个步骤,分别是预处理,编译,汇编和链接。下面以最基本的helloworld来简单了解一下编译的过程。
1.预编译
预编译又称为预处理,是做些代码文本的替换工作。处理#开头的指令,比如拷贝#include包含的文件代码,#define宏定义的替换,条件编译等,就是为编译做的预备工作的阶段,主要处理#开始的预编译指令。
gcc -E hello.c -o hello.i
生成的hello.i仍然是个文本文件
cat@lenovo:~/blog/0718$ ls hello.c cat@lenovo:~/blog/0718$ gcc -E hello.c -o hello.i cat@lenovo:~/blog/0718$ file hello.i hello.i: UTF-8 Unicode C program text
2.编译
把用高级程序设计语言书写的源程序,翻译成汇编语言cat@lenovo:~/blog/0718$ ls hello.c hello.i cat@lenovo:~/blog/0718$ gcc -S hello.i cat@lenovo:~/blog/0718$ ls hello.c hello.i hello.s cat@lenovo:~/blog/0718$ cat hello.s .file "hello.c" .section .rodata .LC0: .string "hello world" .text .globl main .type main, @function main: .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$ ls hello.c hello.i hello.s cat@lenovo:~/blog/0718$ gcc -c hello.s cat@lenovo:~/blog/0718$ ls hello.c hello.i hello.o hello.s cat@lenovo:~/blog/0718$ file hello.o hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
4.链接
将生成的目标文件和其所依赖的库文件进行连接,生成一个可执行文件cat@lenovo:~/blog/0718$ gcc hello.o cat@lenovo:~/blog/0718$ ls a.out hello.c hello.i hello.o hello.s cat@lenovo:~/blog/0718$ ./a.out hello world
相关文章推荐
- 开放Linux端口
- linux(CentOS) 下mysql自动备份
- Linux NFS服务器的安装与配置
- CentOS(二)--JDK的安装和配置
- Linux zip/unzip 命令
- CentOS(一)--硬盘、分区、格式化、卷管理、挂载
- Linux用户与用户组的详解
- Linux下进程信息的深入分析
- linux后端诊断与调试技术
- 如何让Linux下非root用户程序使用小于1024端口
- Linux下抓取log的方法
- linux防火墙iptables原理及使用
- Install xrdp on CentOS 7 / RHEL 7
- Linux面试题汇总答案
- Linux进程的虚拟地址空间
- Ubuntu安装arm-linux-gcc编译工具
- Linux-->问题解决-->/dev/mapper/VolGroup-lv_root: unexpected inconsistency; run fsck manually
- 简单重置Centos服务器中Mysql的root密码
- Linux0.11内核--进程的调度(就绪态和运行态之间的转换)
- linux进程---sigaction 函数