本文主要是介绍C语言.编译过程-Ese .iso,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
gcc 编译过程
从 hello.c 到 hello(或 a.out)文件, 必须历经 hello.i、 hello.s、 hello.o,最后才得到 hello(或
a.out)文件,分别对应着预处理、编译、汇编和链接 4 个步骤,整个过程如图 10.5 所示。
这 4 步大致的工作内容如下:
(1) 预处理, C 编译器对各种预处理命令进行处理,包括头文件包含、宏定义的扩
展、条件编译的选择等;
(2) 编译,将预处理得到的源代码文件,进行“翻译转换”,产生出机器语言的目标
程序,得到机器语言的汇编文件;
(3) 汇编,将汇编代码翻译成了机器码,但是还不可以运行;
(4) 链接,处理可重定位文件,把各种符号引用和符号定义转换成为可执行文件中
的合适信息,通常是虚拟地址。
================================================== gcc的各种编译 ====================================================
编译过程:
预编译 —》把 头文件 ,宏定义,条件编译。。。进行替换扩展
编译 —》检查语法词法,然后将代码翻译成汇编指令
汇编 —》把汇编指令换成机器指令
链接 —》程序中使用到了某些库文件,在这个时候需要链接上
gcc hello.c -o hello —>将 hello.c 编译生成一个可执行文件 hello ,有时候这个不是我们想要的结果
-E Preprocess only; do not compile, assemble or link 预处理 ,不进行 编译 汇编 链接
-S Compile only; do not assemble or link 预处理 编译 ,不进行 汇编 链接
-c Compile and assemble, but do not link 预处理 编译 汇编,不进行 链接
-o Place the output into
//ESc iso
有时候只需要预编译: —》在编译命令后面加上 -E ,后面生成 .i 文件
gcc hello.c -o hello.i -E
gcc -E hello.c -o hello.i
有时候只需要 编译: —》在编译命令后面加上 -S ,后面生成 .s 文件
gcc hello.c -o hello.s -S
gcc -S hello.c -o hello.s
gcc hello.i -o hello.s -S
有时候只需要 汇编: —》在编译命令后面加上 -c ,后面生成 .o 文件(这个文件看不懂)
gcc hello.s -o hello.o -c
gcc -c hello.s -o hello.o
gcc -c hello.c -o hello.o
最后将 .o 文件链接成为可执行文件
gcc hello.o -o hello
gec@ubuntu:/mnt/hgfs/share/day03/code$ gcc hello.c -o hello.i -E
gec@ubuntu:/mnt/hgfs/share/day03/code$ gcc hello.i -o hello.s -S
gec@ubuntu:/mnt/hgfs/share/day03/code$ gcc -c hello.s -o hello.o
gec@ubuntu:/mnt/hgfs/share/day03/code$ gcc hello.o -o hello
gec@ubuntu:/mnt/hgfs/share/day03/code$ ./hello
hello C:6
gec@ubuntu:/mnt/hgfs/share/day03/code$
提示:如果有一天你的程序崩溃,怎么弄都无法解决,就要去研究汇编指令 ,那么我们就需要实现反汇编
gcc 编译器的反汇编指令:objdump ,常用的参数如下:
-d, --disassemble Display assembler contents of executable sections
-D, --disassemble-all Display assembler contents of all sections
0000000000400526 :
400526: 55 push %rbp //压栈
400527: 48 89 e5 mov %rsp,%rbp
40052a: c7 45 f8 05 00 00 00 movl $0x5,-0x8(%rbp)
400531: c7 45 fc 06 00 00 00 movl $0x6,-0x4(%rbp)
400538: 8b 55 f8 mov -0x8(%rbp),%edx
40053b: 8b 45 fc mov -0x4(%rbp),%eax
40053e: 01 d0 add %edx,%eax
400540: 5d pop %rbp //出栈
400541: c3 retq
0000000000400542 :
400542: 55 push %rbp
400543: 48 89 e5 mov %rsp,%rbp
400546: 48 83 ec 20 sub $0x20,%rsp
40054a: 89 7d ec mov %edi,-0x14(%rbp)
40054d: 48 89 75 e0 mov %rsi,-0x20(%rbp)
400551: be 06 00 00 00 mov $0x6,%esi
400556: bf 04 06 40 00 mov $0x400604,%edi
40055b: b8 00 00 00 00 mov $0x0,%eax
400560: e8 9b fe ff ff callq 400400 printf@plt
400565: b8 00 00 00 00 mov $0x0,%eax
40056a: e8 b7 ff ff ff callq 400526
40056f: 89 45 fc mov %eax,-0x4(%rbp)
400572: b8 00 00 00 00 mov $0x0,%eax
400577: c9 leaveq
400578: c3 retq
400579: 0f 1f 80 00 00 00 00 nopl 0x0(%rax)
0000000000400430 <_start>:
400430: 31 ed xor %ebp,%ebp
400432: 49 89 d1 mov %rdx,%r9
400435: 5e pop %rsi
400436: 48 89 e2 mov %rsp,%rdx
400439: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
40043d: 50 push %rax
40043e: 54 push %rsp
40043f: 49 c7 c0 f0 05 40 00 mov $0x4005f0,%r8
400446: 48 c7 c1 80 05 40 00 mov $0x400580,%rcx
40044d: 48 c7 c7 42 05 40 00 mov $0x400542,%rdi
400454: e8 b7 ff ff ff callq 400410 __libc_start_main@plt
400459: f4 hlt
40045a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
这篇关于C语言.编译过程-Ese .iso的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!