Linux动态库加载失败/设置运行时搜索路径/linux中查看动态库的查找搜寻路径

程序运行时有以下错误
./dom1: error while loading shared libraries: libmemcached.so.11: cannot open shared object file: No such file or directory
动态库加载失败

1、首先应该使用命令查看所加载的库

 ldd ./dom1
	linux-vdso.so.1 =>  (0x00007ffdb4368000)
	libmemcached.so.11 => not found
	libc.so.6 => /lib64/libc.so.6 (0x00007f4174962000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f4174d33000)

看看所加载的库
2、使用命令查看加载库时所搜索的路径

LD_DEBUG=libs ./dom1 -v
46688:	find library=libmemcached.so.11 [0]; searching
     46688:	 search cache=/etc/ld.so.cache
     46688:	 search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64		(system search path)
     46688:	  trying file=/lib64/tls/x86_64/libmemcached.so.11
     46688:	  trying file=/lib64/tls/libmemcached.so.11
     46688:	  trying file=/lib64/x86_64/libmemcached.so.11
     46688:	  trying file=/lib64/libmemcached.so.11
     46688:	  trying file=/usr/lib64/tls/x86_64/libmemcached.so.11
     46688:	  trying file=/usr/lib64/tls/libmemcached.so.11
     46688:	  trying file=/usr/lib64/x86_64/libmemcached.so.11
     46688:	  trying file=/usr/lib64/libmemcached.so.11
     46688:	
./dom1: error while loading shared libraries: libmemcached.so.11: cannot open shared object file: No such file or directory


可以看出大多在lib64中搜索,由于我没有lib64这个目录我的是lib目录所以就报加载出错。
解决方案
1、修改搜索目录
2、给你lib中的库生成一个lib64的软连接 就可以解决

3,设置运行时搜索路径的方式,使用-rpath或-R连接选项来指定运行时搜索路径,其优先级高于LD_LIBRARY_PATH。

$ g++ -o dom1 -L. -I. main.cpp -Wl,-rpath .
// 或
$ g++ -o dom1 -L. -I. main.cpp -Wl,-rpath=.
// 或
$ g++ -o dom1-L. -I. main.cpp -Wl,-R.

你可能感兴趣的:(Linux/Win相关学习)