全面复习linux C语言系列--第二讲--GCC编译器
1.编译过程简述:
1.1 预编译
1.1.1 宏定义指令
#define #undef
1.1.2 条件编译指令
例子:
debug , debug.c 内容如下
#include
main()
{
#ifdef DEBUG
fprintf(stderr,"debug infomation \n");
#endif
}
在测试环境上 我们使用下面的命令编译:
cc debug.c -DDEBUG -o debug
他的执行结果:
[root@xhu-vm 5]# ./debug
debug infomation
[root@xhu-vm 5]#
我们看到了debug 信息
上了生产环境,这样编译:
[root@xhu-vm 5]# cc debug.c -o debug
执行结果:
[root@xhu-vm 5]# ./debug
[root@xhu-vm 5]#
没有输出debug信息
1.2 编译
1.3 优化和汇编
2.链接过程
2.1 在debug的例子上在加上一句 打印的测试代码,使用 编译-链接-执行 来完成这个例子
#include
main()
{
#ifdef DEBUG
fprintf(stderr,"debug infomation \n");
#endif
fprintf(stderr,"just a test!\n");
}
a. 使用 [root@xhu-vm 5]# gcc -c debug.c 生成.o 目标文件 debug.o
b. 使用 [root@xhu-vm 5]# gcc debug.o -o debug 将目标文件链接成可执行文件
c. 查看结果
[root@xhu-vm 5]# ./debug
just a test!
[root@xhu-vm 5]#
d.使用dumpobj 反编译.o 目标文件
[root@xhu-vm 5]# objdump -d debug.o
debug.o: file format elf32-i386
Disassembly of section .text:
00000000 :
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 08 sub $0x8,%esp
6: 83 e4 f0 and $0xfffffff0,%esp
9: b8 00 00 00 00 mov $0x0,%eax
e: 83 c0 0f add $0xf,%eax
11: 83 c0 0f add $0xf,%eax
14: c1 e8 04 shr $0x4,%eax
17: c1 e0 04 shl $0x4,%eax
1a: 29 c4 sub %eax,%esp
1c: 83 ec 08 sub $0x8,%esp
1f: 68 00 00 00 00 push $0x0
24: ff 35 00 00 00 00 pushl 0x0
2a: e8 fc ff ff ff call 2b
2f: 83 c4 10 add $0x10,%esp
32: c9 leave
33: c3 ret
[root@xhu-vm 5]#
2.2 动态链接和静态链接
静态链接:[root@xhu-vm 5]# gcc debug.o -o debug
动态链接:[root@xhu-vm 5]# gcc -s debug.c -o debug.s
运行:
[root@xhu-vm 5]# ./debug.s<