本文主要讲解了静态库和动态库的生成,以及.o文件和.so文件的使用。
编辑三个文件,分别是hello.h、hello.c和main.c文件,文件内容如下:
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif //HELLO_H
#include
void hello(const char *name)
{
printf("Hello %s!\n", name);
}
#include "hello.h"
int main()
{
hello("everyone");
return 0;
}
将hello.c编译成为.o文件。【提示:.o文件为可执行文件,又称对象文件。】
gcc -c hello.c
生成后的结果:
由.o 文件创建静态库
ar -crv libhyhello.a hello.o
创建静态库成功:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vo4jmERT-1669126339960)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122214407430.png)]
使用静态库
使用静态库有3中方法,三种方法所用语句不同,但是运行结果相同。
方法一:
gcc -o hello main.c -L. –lhyhello
方法二:
gcc main.c libhyhello.a -o hello
方法三:
gcc -c main.c
gcc -o hello main.o libmyhello.a
我们删除静态库文件试试公用函数hello 是否真的链接到目标文件hello 中了。若删除了静态库仍可以执行,则表明连接到目标文件hello中。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SOpXZxRf-1669126339960)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122214622793.png)]
由.o文件生成动态库。
生成.so文件,命令行:
gcc -o hello main.c -L. -lhyhello
回到根目录下,将文件复制到 /usr/lib 中,在执行hello,不然会报错:
cp libmyhello.so /usr/lib
输出结果如下图所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3iHBqekI-1669126339961)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122214850036.png)]
GCC命令相同时,会先执行.a文件,还是会先执行.so文件?
先删除除.c和.h文件外的所有文件,然后再编译形成.o文件,生成.o文件后,再由.o文件分别生成.a文件和.so文件。执行程序后,发现当静态库和动态库同名时,gcc 命令将优先使用动态库,默认去连/usr/lib 和/lib 等目录中的动态库。
首先编辑四个文件,分别是A1.c、A2.c、A.h和test.c文件,文件内容分别为:
A1.c
#include
void print1(int arg){
printf("A1 print arg:%d\n",arg);
}
A2.c
#include
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
#include "A.h"
int main(){
print1(1);
print2("test");
exit(0);
}
将A1.c和A2.c都生成目标文件
gcc -o A1.c A2.c
生成静态库.a文件
ar crv libhyfile.a A1.o A2.o
使用.a文件创建可执行.exe文件
gcc -o test test.c libhyfile.a
./test
运行结果如下图所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dKFN6Lz5-1669126339961)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122220452278.png)]
生成目标文件
gcc -c -fpic A1.c A2.c
生成共享库.so文件
gcc -shared *.o -o libhysofile.so
创建可执行程序
提示:先要返回到根目录下,赋值文件到 /usr/lib。
sudo cp libhysofile.so /usr/lib
gcc -o test test.c libhysofile.so
./test
运行结果如下图所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QSJzTir0-1669126339961)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122220645550.png)]
静态库的简单应用
编写三个文件,分别是main.c、sub1.c和sub2.c文件,文件内容分别为:
//main
#include
void main()
{float x2x(int a,int b);float x2y(int a,int b);int n,m;n=2;m=3;printf("n*m=%f\n",x2x(n,m));printf("n+m=%f\n",x2y(n,m));
}
//sub1
#include
float x2x(int a,int b)
{return a*b;
}
//sub2
#include
float x2y(int a,int b)
{return a+b;
}
将三个文件编译成为.o文件:
gcc -c main.c sub1.c sub2.c
将sub1.c和sub2.c的目标文件生成一个.a静态库文件:
gcc main.c libxy.a -o xy
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uWiYbkhJ-1669126339962)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122220752540.png)]
动态库的简单应用
将sub1.o和sub2.o的目标文件生成一个.so动态库文件:
gcc -shared -fPIC -o libxy.so sub1.o sub2.o
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P4VzP6T1-1669126339962)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122220818973.png)]
比较.o文件和.so文件的大小
ls-lht xya xyso
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qUnAxbRv-1669126339963)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122220834276.png)]
比较可以看出,静态库的文件较大一些。
①当直接输入命令
gcc -shared -fPIC -o libhyhello.so hello.o
会出现如下图的错误:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8uKQb11x-1669126339963)(C:\Users\GW00293587\AppData\Roaming\Typora\typora-user-images\image-20221122220901693.png)]
此时,需要先输入命令:
gcc -fPIC -c main.c -o hello.o
再输入上方命令。
②rm 直接删除文件
当只是用rm 时,不会出现提示语句,使用rm -i 之后,会询问你是否删除文件。
通过本次学习,基本上掌握了静态库和动态库的创建和使用方法,在此过程中,遇到了很多的困难,但是都是可以通过查资料了解到的,所以并不是不可能解决的问题。