动态库的动态链接(error while loading shared libraries,cannot open shared object file)
来源:互联网 发布:数据库中的decode 编辑:程序博客网 时间:2024/06/05 04:45
$ gcc main.c -g -L. -lstack -o main$ ./main./main:error while loading shared libraries: libstack.so:cannot openshared object file: No such file or directory
linux下编译可执行文件时,经常需要使用到其他的第三方的动态库,通常我们使用-L指定动态库路径,-l指定动态库名字,这样编译没有问题,由于指定了-L.选项,编译器可以在相应目录下找到.so,而运行时却说找不到.so。那么运行时在哪些路径下找动态库呢?首先用ldd命令查看可执行文件依赖于哪些动态库:
$ ldd mainlinux-gate.so.1 => (0xb7f5c000)libstack.so => not foundlibc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7dcf000)/lib/ld-linux.so.2 (0xf7f42000)
ldd模拟执行一遍main,在运行过程中做动态链接,从而得知这个程序依赖于哪些动态库,这些动态库在什么路径下。gcc 调用ld做链接时用-dynamic-linker /lib/ld-linux.so.2 选项指定动态连接器的路径,动态连接器它也像其他动态库一样加载到进程的地址空间中。而另外一个选项-lc说明了需要链接libc库,却没有指出libc库的完整路径,-lstack就是这样,动态库的路径需要运行时由动态连接器/lib/ld-linux.so.2去查找。
设置动态链接器搜索动态库的路径
上面的例子中,动态连接器找到的路径是:/lib/tls/i685/cmov/.libc.so.6,而libstack的路径没有找到,无法完成链接。那么动态链接器会到哪些目录下搜索动态库呢?从ld.so(8)可以查到动态库路径的搜索顺序:1. 首先在环境变量LD\_LIBRARY\_PATH保存的路径中查找。2. 然后从缓存文件/etc/ld.so.cache中查找。这个缓存文件是由ldconfig命令读取配置文件/etc/ld.so.conf生成的,这个后面会详细解释。3. 如果上述步骤都找不到,则到默认的系统库文件目录中查找,先是/usr/lib 然后是/lib。
一、修改环境变量法
$ LD_LIBRARY_PATH = /home/akaedu/testdir ./main
这个方法只适合在开发调试中临时用一下,设置环境变量法通常是不推荐的。
二、修改配置文件法
这是最常用的方法。把libstack.so所在的目录的绝对路径(比如/home/akaedu/somedir)添加到配置文件/etc/lib.so.conf(该文件中每个路径占一行),然后运行ldconfig:
$ sodu ldconfig -v
ldconfig命令可以生成/etc/ld.so.cache缓存文件,动态连接器就从这个缓存文件中搜索动态库。
三、文件拷贝法
把libstack.so拷到/usr/lib或/lib目录,这样可以确保动态链接器能找到这个动态库。
四、写死路径法
$ gcc main.c -g -L. -lstack -o main -W1 -rpath, /home/akaedu/somedir
注意选项-W1, -rpath, /home/akaedu/somedir, -W1 表示gcc传给链接器的选项。这种方法不推荐,把搜索路径写死在可执行文件中是一种硬代码的做法。
转自:http://akaedu.github.io/post/09/9.3.html- 动态库的动态链接(error while loading shared libraries,cannot open shared object file)
- error while loading shared libraries cannot open shared object file
- error while loading shared libraries| cannot open shared object file
- Error While Loading Shared Libraries, Cannot Open Shared Object File
- Error While Loading Shared Libraries, Cannot Open Shared Object File
- error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: 的解决办法
- error while loading shared libraries: xxx.so.0:cannot open shared object file:的错误解决
- error while loading shared libraries: libpthread.so.0: cannot open shared object file: No such file
- error while loading shared libraries: libXXX.so.x: cannot open shared object file: No such file
- error while loading shared libraries: libssl.so.4: cannot open shared object file: No such file.....
- error while loading shared libraries: libiw.so.29: cannot open shared object file: No such file
- error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file
- error while loading shared libraries: XXX.so: cannot open shared object file: No such file...
- jps: error while loading shared libraries: libjli.so: cannot open shared object file: No such file..
- error while loading shared libraries: libpthread.so.0: cannot open shared object file: No such file
- error while loading shared libraries: libcudart.so.6.5: cannot open shared object file: No such file
- Error: while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
- Linux下出现error while loading shared libraries,...cannot open shared object file: No such file..
- JavaScript学习要点(十三)
- python爬虫(二)--了解deque
- C语言:用链表实现一元多项式的加法
- 双向循环链表和树
- 洛谷1028 数的计算
- 动态库的动态链接(error while loading shared libraries,cannot open shared object file)
- SPI相关理论知识总结
- CSS基础-17CSS盒子模型-外边距合并
- Android设计模式系列-单例模式
- 9.谓词(Predicate)
- hdu1299
- UVA - 11020 - Efficient Solutions (multiset实现BST)
- POI-----POI操作Excel-5、公式
- SVM理解