南邮 | Linux实验六:Linux/C 开发环境 函数库和 glibc

静态函数库的创建和使用

共享函数库的创建和使用

glibc函数库的使用

1. 静态函数库的创建与使用

项目中模块的构成见实验五,源代码,分别为add.c,sum.c,mylib.h和app.c.

使用编辑器 vi 编辑下列脚本

static_lib:add.c sum.c 
	gcc -c add.c
	gcc -c sum.c
	ar -cru libdemo.a    add.o sum.o
app_static: libdemo.a
	gcc  -static  app.c  -L.  -ldemo  -o  app_static

将下列脚本保存至文件static_app中

通过下列操作可产生静态函数库

$ make -f static_app static_lib  //static_lib表示产生静态库的目标
$ ls libdemo.a   //显示静态函数库文建

使用静态函数库产生可执行目标代码

$ make -f static_app app_static  //app_static表示产生可执行目标代码
$ ./app_static  //运行可执行代码

2. 共享函数库的创建和使用

使用vi编辑下列脚本

shared_lib:add.c sum.c 
	gcc -fPIC -c add.c
	gcc -fPIC -c sum.c
	gcc -shared add.o sum.o -o libdemo.so
app_shared:libdemo.so
	gcc    app.c  -L.  -ldemo  -o  app_shared

将上述脚本保存至文件shared_app中

通过下列操作产生共享函数库libdemo.so

$ make -f shared_app  shared_lib //目标shared_lib表示产生共享函数库
$ ls libdemo.so   //查看共享函数库文件

使用共享函数库,据操作如下:

$ make -f shared_app app_shared //目标app_shared表示产生可执行代码app
$ ls -l app_static app_shared //比较两个文件的大小
$ ./app_shared  //执行

3. glibc函数库

(1) 通过软中断调用Linux内核功能

使用编辑器vi编辑下列源代码

.equ	sys_EXIT, 1		    
.equ	sys_WRITE, 4	    
.equ	dev_STDOUT, 1	    

	.section	.data    
msg:	.ascii	" Hello \n"	
len:	.long	. - msg	 

	.section	.text  
_start:	 
	movl	$sys_WRITE, %eax	
	movl	$dev_STDOUT, %ebx	
	movl	$msg, %ecx		
	movl	len, %edx		
	int	$0x80			
 
	movl	$sys_EXIT, %eax		
	movl	$0, %ebx		
	int	$0x80

将上述代码保存至 demo.s,通过下列操作生成可执行代码

$ as demo.s -o demo.o    //编译生成中间目标代码
$ ld demo.o -o demo  //链接生成可执行代码
$ ./demo    //运行demo程序
(2) 通过glic函数库使用Linux!内核功能

使用编辑器vi编辑下列源代码

#include<stdio.h>
int main(void)
{
printf("hello\n");
}

将上述代码保存至test.c中,通过下列操作生成可执行代码

$ gcc -o test test.c
$ ./test //执行test

比较demo和test执行结果.

4. 思考题

  1. glibc函数库的作用

  2. 分析静态函数库与共享函数库各自的优缺点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wonz

创作不易,一块就行。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值