静动态库的方式

张开发
2026/5/6 16:50:10 15 分钟阅读
静动态库的方式
1.程序编译的过程a.预处理头文件的包含(xxx.h)函数的声明和宏的定义注释的删除。b.编译将C语言编译为汇编语言。c.汇编将汇编语言翻译为二进制生成二进制的目标文件。d.链接将生成的目标文件与动(静)态库中的代码(函数的定义)结合。// filename:makefile mycode.out:mycode.o g -o mycode.out mycode.o // 发生链接的过程 mycode.o:mycode.s g -c mycode.s -o mycode.o mycode.s:mycode.i g -S mycode.i -o mycode.s mycode.i:mycode.cpp g -E mycode.cpp -o mycode.i .PHINY:clean clean: rm -r mycode.out;2.静动态库的差异默认是动态链接动态库当使用静态库链接时只需在链接时添加 -staticmycode.out:mycode.o g -o mycode.out mycode.o -static结果可以对比出动态mycode.out的大小时8656静态mycode.out的大小是2314032原因静态链接在链接时将静态库的全部数据合并到最终的可执行文件。动态链接在连接时只记录需要数据在lib.so中的位置判断是否存在运行时再直接找到。3.静态库的使用1.命名格式libxxxxx.a 其中xxxxx是静态库的名字 .a 表示为静态链接2.实现一个静态库​ ​ // math.h #includeiostream double myadd(double x,double y); double mysub(double x,double y); double mymul(double x,double y); double mydiv(double x,double y); //staticmath.cpp double myadd(double x,double y) {return xy;} double mysub(double x,double y) {return x-y;} double mymul(double x,double y) {return x*y;} double mydiv(double x,double y) {return x/y;} // 生成静态库 g -c staticmath.cpp -o staticmath.o ar rcs libstaticmath.a staticmath.o //test.cpp进行测试 #includeiostream #includemath.h using namespace std; int main() { int x8,y2; coutxymyadd(x,y)endl; coutx-ymysub(x,y)endl; coutx*ymymul(x,y)endl; coutx/ymydiv(x,y)endl; return 0; } // g test.cpp -o test.out -L. -lstaticmath 生成可执行文件 结果 ./test.out xy10 x-y6 x*y16 x/y4-L表示静态库所在的目录-l表示静态库名3.添加静态库的内容新加一个比较函数​ // compare.h int mymax(int x,int y); int mymin(int x,int y); //compare.cpp int mymax(int x,int y) { return x y ? x :y ; } int mymin(int x,int y) { return x y ? x : y ; } // g -c compare.cpp -o compare.o // ar r libstaticmath.a compare.o // 将新的目标函数加入库中 // test.cpp #includeiostream #includemath.h #includecompare.h using namespace std; int main() { double x8,y2; coutmax(x,y)mymax((int)x,(int)y)endl; coutmin(x,y)mymin((int)x,(int)y)endl; coutxymyadd(x,y)endl; coutx-ymysub(x,y)endl; coutx*ymymul(x,y)endl; coutx/ymydiv(x,y)endl; return 0; } g -o test.out test.cpp -L . -lstaticmath ./test.out 结果: max(x,y)8 min(x,y)2 xy10 x-y6 x*y16 x/y4 ​4.系统找头文件的方式1. include类型找 -I我们指定的路径和 系统标准路径。2.include 类型先找当前目录下再找指定路径和系统标准路径。例使用相对路径编译[root code_cpp]# g -o test.out test.cpp -I ../code_h -L ../code_a -lstaticmath [root code_cpp]# ll total 28 -rw-r--r-- 1 root root 106 Apr 12 16:14 compare.cpp -rw-r--r-- 1 root root 183 Apr 12 15:38 staticmath.cpp -rw-r--r-- 1 root root 371 Apr 12 16:16 test.cpp -rwxr-xr-x 1 root root 13144 Apr 12 16:50 test.out [rootVM-8-15-opencloudos code_cpp]# ./test.out max(x,y)8 min(x,y)2 xy10 x-y6 x*y16 x/y45.动态库的使用1.命名格式libxxxxx.so 其中xxxxx是静态库的名字 .so 表示为软链接2.实现动态库. ├── code_a │ ├── libdynmath.so │ └── libstaticmath.a ├── code_cpp │ ├── compare.cpp │ ├── staticmath.cpp │ └── test.cpp ├── code_h │ ├── compare.h │ └── math.h ├── code_o │ ├── compare.o │ ├── dynmath.o │ └── staticmath.o ├── makefile └── test.cpp g -fPIC -c staticmath.cpp -o dynmath.o g -shared -o libdyn.so dynmath.o g -o test.out test.cpp -I code_h -L /code_a -ldynmath 失败找不到文件的位置缺少路径 /usr/bin/ld: cannot find -ldynmath: No such file or directory collect2: error: ld returned 1 exit status 两种方法 1.将库放在usr/lib中 2.在/etc/ld.so.conf文件添加绝对路径 vim ld.so.conf ------ 令起一行添加/root/tmp/code_a [rootVM-8-15-opencloudos tmp]# vim /etc/ld.so.conf [rootVM-8-15-opencloudos tmp]# ldconfig // 将ld.so.conf数据加载入ld.so.cache文件 [rootVM-8-15-opencloudos tmp]# ./test.out xy10 x-y6 x*y16 x/y43.添加动态库的内容动态库添加不能像静态库在原有的基础上添加需要重新全部打包g -fPIC -c compare.cpp -o dyncompare.o g -shared -o libdynmath_com.so dynmath.o dyncompare.o g -o test.out test.cpp -I code_h -L code_a -ldynmath_com . ├── code_a │ ├── libdynmath_com.so │ ├── libdynmath.so │ └── libstaticmath.a ├── code_cpp │ ├── compare.cpp │ ├── staticmath.cpp │ └── test.cpp ├── code_h │ ├── compare.h │ └── math.h ├── code_o │ ├── compare.o │ ├── dyncompare.o │ ├── dynmath.o │ └── staticmath.o ├── makefile ├── test.cpp └── test.out ./test.out 结果 max(x,y)8 min(x,y)2 xy10 x-y6 x*y16 x/y4

更多文章