c语言编译器.l 和.y文件,全面温习linux C语言系列-第二讲-GCC编译器

本文深入介绍了Linux环境下GCC编译器的工作原理和使用,包括预编译、编译、优化和汇编过程,以及链接阶段的动态链接和静态链接。通过实例详细展示了如何创建和使用静态库、动态链接库,以及动态加载库文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

全面复习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<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值