博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux静态库和动态库
阅读量:4611 次
发布时间:2019-06-09

本文共 2018 字,大约阅读时间需要 6 分钟。

  函数库是常用公共函数的集合,分为动态库和静态库。

  静态库:程序编译时被链接到目标文件中,程序运行时就不需要此函数库的存在。(不推荐,会重复链接库)

  动态库:程序编译时不链接到目标文件中,程序运行时才链接,所以需要函数库的存在。(推荐方式)

1:静态库

  ./test/libhello/hello1.c

#include 
void print1(){ printf("hello1****\n");}

  ./test/libhello/hello2.c

#include 
void print2(){ printf("hello2****\n"); }

  ./test/test.c

#include
extern void print1(); extern void print2();//避免 warning: implicit declaration of function "function name" int main(int argc,char **argv){ print1(); print2(); return 0;}

  1.1 将库文件(hello1.c,hello2.c)编译为.o文件

  ./test/libhello:

    gcc -c hello1.c hello2.c  --> hello1.o hello2.o

  -c : 只编译不链接

  1.2 用.o文件创建静态库

  静态库命名规则:lib+库名.a 比如要创建库hello,则静态库名字必须为libhello.a

  使用命令:

  ./test/libhello:

    ar -r libhello.a hello1.o hello2.o  --> libhello.a

  1.3 使用静态库

  test.c文件需要此静态库,只需要在编译test.c时gcc链接此静态库,静态库中的函数就会在目标文件中声明。

  ./test:

    gcc -o test test.c -L ./libhello -lhello  --> test

  -L:指定gcc在什么位置去链接hello库文件,不指定默认在/usr/lib中去找

  -l(库名):库名hello就对应了静态库libhello.a

  因为链接的是静态库,编译时库函数已经被链接进了目标文件test中,所以删除库文件libhello.a后test程序还是可以正常运行.

2:动态库

  2.1 用.o文件生成动态库

  动态库命名规则:lib+库名.so 比如要创建库hello,则静态库名字必须为libhello.so

  使用命令:

  ./test/libhello:

    gcc -shared -fPCI -o libhello.so hello1.o hello2.o --> libhello.so

    cp libhello.so  /usr/lib (因为动态库是运行时才链接的,链接的默认位置是‘/usr/lib’,所以将.so拷贝至/usr/lib)

  2.2 使用动态库

  ./test:

    gcc -o test test.c -L ./libhello -lhello

  *:也可以在编译的时候指定链接的动态库的位置:gcc -o test test.c -L ./libhello/ -lhello -Wl,-rpath,./libhello/

  这样就不用把库文件拷贝到/usr/lib目录下,目标程序会自动去./libhello文件下去链接

  删除库文件libhello.so执行将出错:

    error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory

3:总结

  1:当静态库和动态库同名时(libhello.a和libhello.so),默认使用的是动态库

  2:当一个库文件中有两个同名函数时,应用程序默认链接最前面哪个(比如hello1.o有print(),hello2.o有print(),在制作libhello.a时,ar -f libhello.a hello1.o hello2.o 这时会使用hello1.c中的print()函数)-->(在制作动态库时会提示命名冲突multiple definition of `print')

转载于:https://www.cnblogs.com/Flychown/p/6612030.html

你可能感兴趣的文章
mysql-----------limit优化
查看>>
JAvaScript:JS数组元素去重的方法
查看>>
MATLAB 的数据导入与导出
查看>>
python logging模块
查看>>
rsync 进行本地拷贝
查看>>
npm install总结 (--savenpm install --save与npm install --save-dev区别及其他相关)
查看>>
css在线压缩工具!
查看>>
Lifting the Stone
查看>>
poj2823单调队列
查看>>
P4238 【模板】多项式求逆 ntt
查看>>
Linux下chkconfig命令详解
查看>>
视频转gif
查看>>
KakfaSpout自定义scheme
查看>>
java基础之循环应用(打印三角形、菱形)
查看>>
loadrunner监控tomcat 代码及遇到的问题
查看>>
广度优先搜索 codevs 2806 红与黑
查看>>
人生第一次博客
查看>>
mysql 动态行转列
查看>>
矢量图标
查看>>
类及成员的属性——静态成员与实例成员
查看>>