嵌入式四周实验

目录

一、用 gcc 生成 .a 静态库和 .so 动态库

1.编辑生成hello.h、hello.c和main.c

2.将hello,c编译成.o文件

3.由.o 文件创建静态库

4.程序中使用静态库

 5.由.o文件创建动态库文件

 6.在程序中使用动态库

7.当静态库和动态库同名时,gcc 命令会使用哪个库文件

二、Linux 下静态库.a 与动态库.so 库文件的生成与使用

代码:

 1.静态库.a 文件的生成与使用。

2、共享库.so 文件的生成与使用

三、在第一次作业的程序代码基础进行改编

代码:

 1.静态库

2.动态库、

四、总结

五、参考文献


一、用 gcc 生成 .a 静态库和 .so 动态库

1.编辑生成hello.h、hello.c和main.c

 hello.h代码:

#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif//HELLO_H


hello.c代码:

#include<stdio.h>
void hello(const char *name)
{
	printf("HELLO %s\n",name);
}


main.c代码:

#include"hello.h"
int main()
{
	hello("everyone");
	return 0;
}


2.将hello,c编译成.o文件

无论静态库,还是动态库,都是由.o 文件创建的。因此,我们必须将源程序 hello.c 通过 g cc 先编译成.o 文件。在系统提示符下键入以下命令得到 hello.o 文件

3.由.o 文件创建静态库

静态库文件名的命名规范是以 lib 为前缀,紧接着跟静态库名,扩展名为.a。例如:我们将 创建的静态库名为 myhello,则静态库文件名就是 libmyhello.a。在创建和使用静态库时, 需要注意这点。创建静态库用 ar 命令。在系统提示符下键入以下命令将创建静态库文件 libmyhello.a。

4.程序中使用静态库

方法一: 

# gcc -o hello main.c -L. –lmyhello

自定义的库时,main.c 还可放在-L.和 –lmyhello 之间,但是不能放在它俩之后,否则会提 示 myhello 没定义,但是是系统的库时,如 g++ -o main(-L/usr/lib) -lpthread main.cpp 就不出错。

方法二:

方法三:

 先生成 main.o: gcc -c main.c

再生成可执行文件: gcc -o hello main.o libmyhello.a 动态库连接时也可以这样做。

# ./hello

Hello everyone!

# 我们删除静态库文件试试公用函数 hello 是否真的连接到目标文件 hello 中了。

# rm libmyhello.a    rm: remove regular file `libmyhello.a'? y

# ./hello

Hello everyone!

# 程序照常运行,静态库中的公用函数已经连接到目标文件中了。

 5.由.o文件创建动态库文件

动态库文件名命名规范和静态库文件名命名规范类似,也是在动态库名增加前缀 lib,但其 文件扩展名为.so。例如:我们将创建的动态库名为 myhello,则动态库文件名就是 libmyh ello.so。用 gcc 来创建动态库。 在系统提示符下键入以下命令得到动态库文件 libmyhello.so。

# gcc -shared -fPIC -o libmyhello.so (-o必不可少)

 6.在程序中使用动态库

 在程序中使用动态库和使用静态库完全一样,也是在使用到这些公用函数的源程序中包含 这些公用函数的原型声明,然后在用 gcc 命令生成目标文件时指明动态库名进行编译。我 们先运行 gcc 命令生成目标文件,再运行它看看结果。

# gcc -o hello main.c -L. -lmyhello (或 #gcc main.c libmyhello.so -o hello 不会出错(没有 libmyhello.so 的话,会出错),但是 接下来./hello 会提示出错,因为虽然连接时用的是当前目录的动态库,但是运行时,是到 /usr/lib 中找库文件的,将文件 libmyhello.so 复制到目录/usr/lib 中就 OK 了)

# ./hello

./hello: error while loading shared libraries: libmyhello.so: cannot open shar

ed object file: No such file or directory

# 哦!出错了。快看看错误提示,原来是找不到动态库文件 libmyhello.so。程序在运行时, 会在/usr/lib 和/lib 等目录中查找需要的动态库文件。若找到,则载入动态库,否则将提 示类似上述错误而终止程序运行。我们将文件 libmyhello.so 复制到目录/usr/lib 中,再试 试。

# mv libmyhello.so /usr/lib

# ./hello

Hello everyone!

# 成功了。这也进一步说明了动态库在程序运行时是需要的

7.当静态库和动态库同名时,gcc 命令会使用哪个库文件

先删除除.c 和.h 外的所有文件,恢复成我们刚刚编辑完举例程序状态。

# rm -f hello hello.o /usr/lib/libmyhello.so

# ls

hello.c hello.h main.c

 再来创建静态库文件 libmyhello.a 和动态库文件 libmyhello.so。

# gcc -c hello.c

# ar -cr libmyhello.a hello.o (或-cvr )

# gcc -shared -fPIC -o libmyhello.so hello.o

# ls

hello.c hello.h hello.o libmyhello.a libmyhello.so main.c

# 通过上述最后一条 ls 命令,可以发现静态库文件 libmyhello.a 和动态库文件 libmyhello.s o 都已经生成,并都在当前目录中。然后,我们运行 gcc 命令来使用函数库 myhello 生成目 标文件 hello,并运行程序 hello。

# gcc -o hello main.c -L. –lmyhello (动态库和静态库同时存在时,优先使用动态库,当然,如果直接 #gcc main.c libmyhello.a -o hello 的话,就是指定为静态库了)

# ./hello

./hello: error while loading shared libraries: libmyhello.so: cannot open shar

ed object file: No such file or directory

# 从程序 hello 运行的结果中很容易知道,当静态库和动态库同名时,gcc 命令将优先使用动 态库,默认去连/usr/lib 和/lib 等目录中的动态库,将文件 libmyhello.so 复制到目录/usr/lib 中即可。

二、Linux 下静态库.a 与动态库.so 库文件的生成与使用

代码:

A1.c

#include <stdio.h>
void print1(int arg)
{
printf("A1 print arg:%d\n",arg);
}

A2.c

#include <stdio.h>
void print2(char *arg)
{
printf("A2 printf arg:%s\n", arg);
}

A.h

#ifndef A_H
#define A_H
void print1(int);
void print2(char *);
#endif

test.c

#include <stdlib.h>
#include "A.h"
int main()
{
print1(1);
print2("test");
exit(0);
}

 1.静态库.a 文件的生成与使用。

1.1、生成目标文件(xxx.o)

1.2、生成静态库.a 文件

1.3、使用.a 库文件,创建可执行程序(若采用此种方式,需保证生成的.a 文件与.c 文件保 存在同一目录下,即都在当前目录下)

2、共享库.so 文件的生成与使用

2.1、生成目标文件(xxx.o() 此处生成.o 文件必须添加"-fpic"(小模式,代码少),否则在生成.so 文件时会出错)

2.2、生成共享库.so 文件

2.3、使用.so 库文件,创建可执行程序

三、在第一次作业的程序代码基础进行改编

代码:

 sub1.c

float x2x(int a,int b)
{
	float c=0;
	c=a+b;
	return c;
}

sub2.c

float x2y(int a,int b)
{
	float c=0;
	c=a/b;
	return c;
}

sub.h

#ifndef SUB_H
#define SUB_H
float x2x(int a,int b);
float x2y(int a,int b);
#endif

main.c

#include<stdio.h>
#include"sub.h"
void main()
{
	int a,b;
	printf("Please input the value of a:");
	scanf("%d",&a);
	printf("Please input the value of b:");
	scanf("%d",&b);
	printf("a+b=%.2f\n",x2x(a,b));
	printf("a/b=%.2f\n",x2y(a,b));
}

 1.静态库

2.动态库、

 比较动态库与静态库:

 通过比较发现动态库的文件比静态库大得多。

四、总结

通过学习并且使用静态库与动态库可以清楚地发现两者的不同。

五、参考文献

Linux基础——gcc编译、静态库与动态库(共享库)_daidaihema的博客-CSDN博客_gcc编译链接静态库和动态库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值