在Linux上,编译、链接或运行一些程序时,总有机会遇到一些链接库找不到(或被调用函数未定义)之类的问题。遇到这类链接库相关的问题,次数多了也通过各种方法解决过类似问题,所以简单记录一下相关的知识点吧,以便下次遇到问题时能更有条理地迅速地解决问题。
对于动态执行某个程序时,发现某个共享库找不到,则可以用 “ldd <可执行文件名>”命令查看可执行文件链接了哪些系统动态链接库。
1 2 3 4 5 6 7 8 9 10 |
master@jay-ubuntu:~$ ldd /bin/ls linux-vdso.so.1 => (0x00007fff79783000) libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f9a66af6000) librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f9a668ee000) libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f9a666e5000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9a66326000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9a66122000) /lib64/ld-linux-x86-64.so.2 (0x00007f9a66d38000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9a65f04000) libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f9a65cff000) |
man ld
ld combines a number of object and archive files, relocates their data and ties up symbol references. Usually the last step in compiling a program is to run ld.
The linker uses the following search paths to locate required shared libraries:
1. Any directories specified by -rpath-link options.
2. Any directories specified by -rpath options. The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time. Searching -rpath in this way is only supported by native linkers and cross linkers which have been configured with the --with-sysroot option.
3. On an ELF system, for native linkers, if the -rpath and -rpath-link options were not used, search the contents of the environment variable "LD_RUN_PATH".
4. On SunOS, if the -rpath option was not used, search any directories specified using -L options.
5. For a native linker, the search the contents of the environment variable "LD_LIBRARY_PATH".
6. For a native ELF linker, the directories in "DT_RUNPATH" or "DT_RPATH" of a shared library are searched for shared libraries needed by it. The "DT_RPATH" entries are ignored if "DT_RUNPATH" entries exist.
7. The default directories, normally /lib and /usr/lib.
8. For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that file. If the required shared library is not found, the linker will issue a warning and continue with the link.
man gcc
-llibrary
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.
The directories searched include several standard system directories plus any that you specify with -L.
Normally the files found this way are library files---archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with lib and .a and searches several directories.
man ldconfig:
ldconfig creates, updates, and removes the necessary links and cache (for use by the run-time linker, ld.so) 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 (/usr/lib and /lib).
1 2 3 4 5 6 |
master@jay-ubuntu:~$ cat /etc/ld.so.conf include /etc/ld.so.conf.d/*.conf master@jay-ubuntu:~$ cat /etc/ld.so.conf.d/x86_64-linux-gnu.conf # Multiarch support /lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu |
有时,自己安装了某个库,但是发现依然不能用,则需要运行ldconfig更新一下动态链接库的缓存。如果依然无法找到该库,则需要检查一下该库的安装位置是否默认的/usr/lib和/lib,如果不是在默认搜寻路径,需要在/etc/ld.so.conf(及其引入的文件)中添加该库的安装目录。
ldconfig工具建立的共享库缓存文件一般为:/etc/ld.so.cache
1 2 3 4 5 6 7 8 9 |
master@jay-ubuntu:~$ strings /etc/ld.so.cache | grep pthread libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0 libpthread.so.0 /lib/i386-linux-gnu/libpthread.so.0 libgpgme-pthread.so.11 /usr/lib/libgpgme-pthread.so.11 libgpgme++-pthread.so.2 /usr/lib/libgpgme++-pthread.so.2 |
对于一些只有少数特定的程序才会使用的共享库,那么就没有必要加入系统库路径,以免应用程序的共享库之间发生版本冲突。那么,还可以通过设置环境变量LD_LIBRARY_PATH来临时指定应用程序的共享库搜索路径,可以在应用程序的启动脚本里面预先设置 LD_LIBRARY_PATH,指定本应用程序附加的共享库搜索路径,从而让应用程序找到它。
参考资料:
man ld/gcc/ldconfig
http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
http://blog.sina.com.cn/s/blog_67eb1f2f0100mgd8.html
reasons, many people often harrow independently unnoticed. wireless data transmitter Intended for wi-fi programs, the leading supplier usually are battery packs in addition to doesn't need for being of the house or maybe small business electric power brand. The item places strength by battery packs, and so given that battery