编译程序时使用了如下参数指定了链接库的位置,但是执行时提示无法加载共享库。
#gcc test.c -L /usr/local/rrdtool-1.2.30/lib -lrrd
现象:
error while loading libraries:librrd.so.2:cannot open shared object file:No such file or directory
解决方法:
使用ldd命令 查看编译生成的可执行文件发现:
#ldd a.out
命令输出结果:
librrd.so.2 => not found
修改/etc/ld.so.conf文件添加 目录——/usr/local/rrdtool-1.2.30/lib
执行如下命令:
#ldconfig
再次执行如下命令:
#ldd a.out
本身输出:
librrd.so.2 => /usr/local/rrdtool-1.2.30/lib/librrd.so.2
原理:
编译时确实是通过了,因为你编译时指定了编译时需要的有关该共享库的信息。
但是没有指定运行时(run time)所需要的信息;man一把ldconfig命令就知道原因了。
ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). The cache(/etc/ld.so.cache
) is used by the run-time linker, ld.so or ld-linux.so.
需要的动态来接库的目录路径,必须在/etc/ld.so.cache 文件中,而该文件是通过ld.so.conf指定的目录,以及信任目录(/lib and /usr/lib),使用ldconfig命令生成的。ldd命令也查看可执行文件所依赖的动态库信息时,也使用了/etc/ld.so.cache文件,因此在没有将所需要的动态库目录添加到ld.so.conf中且执行ldconfig时,会提示该库"not found"
from:http://blog.163.com/lyzaily@126/blog/static/42438837201222373118749/
#gcc test.c -L /usr/local/rrdtool-1.2.30/lib -lrrd
现象:
error while loading libraries:librrd.so.2:cannot open shared object file:No such file or directory
解决方法:
使用ldd命令 查看编译生成的可执行文件发现:
#ldd a.out
命令输出结果:
librrd.so.2 => not found
修改/etc/ld.so.conf文件添加 目录——/usr/local/rrdtool-1.2.30/lib
执行如下命令:
#ldconfig
再次执行如下命令:
#ldd a.out
本身输出:
librrd.so.2 => /usr/local/rrdtool-1.2.30/lib/librrd.so.2
原理:
编译时确实是通过了,因为你编译时指定了编译时需要的有关该共享库的信息。
但是没有指定运行时(run time)所需要的信息;man一把ldconfig命令就知道原因了。
ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). The cache(/etc/ld.so.cache
) is used by the run-time linker, ld.so or ld-linux.so.
需要的动态来接库的目录路径,必须在/etc/ld.so.cache 文件中,而该文件是通过ld.so.conf指定的目录,以及信任目录(/lib and /usr/lib),使用ldconfig命令生成的。ldd命令也查看可执行文件所依赖的动态库信息时,也使用了/etc/ld.so.cache文件,因此在没有将所需要的动态库目录添加到ld.so.conf中且执行ldconfig时,会提示该库"not found"
from:http://blog.163.com/lyzaily@126/blog/static/42438837201222373118749/