文章目录
前言
函数库分为静态库和动态库两种。静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库。动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入,因此在程序运行时还需要动态库存在。
一、用gcc生成.a静态库和.so动态库
1.编辑生成例子程序 hello.h、hello.c 和 main.c
1) hello.h:
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif //HELLO_H
2) hello.c:
#include <stdio.h>
void hello(const char *name)
{
printf("Hello %s!\n", name);
}
3) main.c:
#include "hello.h"
int main()
{
hello("everyone");
return 0;
}
2.使用gcc编译得到.o 文件
gcc -c hello.c
3.由.o 文件创建静态库
ar -crv libmyhello.a hello.o
4.在程序中使用静态库。
1)gcc -o hello main.c -L. –lmyhello
2)gcc main.c libmyhello.a -o hello
3)
gcc -c main.c
gcc -o hello main.o libmyhello.a
5.由.o 文件创建动态库文件
gcc -shared -fPIC -o libmyhello.so hello.o
6.在程序中使用动态库
gcc -o hello main.c -L. -lmyhello
中间会报错,问题的解决方法:将libmyhello.so复制到目录/usr/lib中。由于运行时,是在/usr/lib中找库文件的。
移动过后,即可正常执行
小结
在执行可执行文件,会报一个错误,可见当静态库和动态库同时存在的时候,程序会优先使用动态库。
二、静态库.a 与.so 库文件的生成与使用
1.编辑文件
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);
}
2.静态库.a 文件的生成与使用。
1) 生成目标文件(xxx.o)
gcc -c A1.c A2.c
2) 生成静态库.a 文件
ar crv libafile.a A1.o A2.o
3) 使用.a 库文件,创建可执行程序
gcc -o test test.c libafile.a
./test
3.共享库.so 文件的生成与使用
1) 生成目标文件(xxx.o) (此处生成.o 文件必须添加"-fpic"(小模式,代码少),否则在生成.so
文件时会出错)
gcc -c -fpic A1.c A2.c
2) 生成共享库.so 文件
gcc -shared *.o -o libsofile.so
3) 使用.so 库文件,创建可执行程序
gcc -o test test.c libsofile.so
执行文件可以发现出现错误,这是由于 linux 自身系统设定的相应的设置的原因,即其只在/lib and /usr/lib 下搜索对应
的.so 文件,故需将对应 so 文件拷贝到对应路径。
sudo cp libsofile.so /usr/lib
成功运行
总结
在两种库的比较中,能够明显看出两者的差别。静态库可以直接调用,而动态库则需要设定文件位置,才能让可执行文件正确识别并执行。可执行文件是通过编译链接获取得到的,利用工具将源码编译得到.o文件,接下来就是将.o文件链接得到可执行文件。