centos学习:动态链接库 .so "dll"?

时间:2021-07-21 15:51:10

一 、生成linux动态链接库

gcc -o libme.so age.c -shared

二、调用动态链接库

[root@localhost study]# gcc -L /root/study -l me daichen.c -o daichen
[root@localhost study]# ls
age.c  age.h  daichen  daichen.c  libme.so  makefile
[root@localhost study]# ./daichen
./daichen: error while loading shared libraries: libme.so: cannot open shared object file: No such file or directory
[root@localhost study]# 
gcc -L /root/ -l me daichen.c -o daichen

三、查询该程序有什么依赖

ldd daichen

四、出现错误 动态链接库连接不上

[root@localhost study]# ./daichen
./daichen: error while loading shared libraries: libme.so: cannot open shared object file: No such file or directory

失败原因是操作系统无法找到libme.so

linux和windows一样 有类似system32的系统库文件夹。各种公共类库都放在这个地方

centos中有个和windows很类似的两个存放公共库的文件夹

/lib 内核级
/usr/lib 用户系统级
/usr/lib64/ 64为系统才有

[root@localhost study]# cp libme.so /lib
[root@localhost study]# ./daichen
./daichen: error while loading shared libraries: libme.so: cannot open shared object file: No such file or directory
[root@localhost study]# ldconfig
[root@localhost study]# ./daichen
is 0x400758 
is 0x400759 
is 0x40075a 
is 0x40075b 
is 0x40075c 
is 0x40075d 
is 0x40075e 
[root@localhost study]# 

ldconfig:更新动态缓存库