关于Linux上的链接库

在Linux上,编译、链接或运行一些程序时,总有机会遇到一些链接库找不到(或被调用函数未定义)之类的问题。遇到这类链接库相关的问题,次数多了也通过各种方法解决过类似问题,所以简单记录一下相关的知识点吧,以便下次遇到问题时能更有条理地迅速地解决问题。
 
对于动态执行某个程序时,发现某个共享库找不到,则可以用 “ldd <可执行文件名>”命令查看可执行文件链接了哪些系统动态链接库。


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).

有时,自己安装了某个库,但是发现依然不能用,则需要运行ldconfig更新一下动态链接库的缓存。如果依然无法找到该库,则需要检查一下该库的安装位置是否默认的/usr/lib和/lib,如果不是在默认搜寻路径,需要在/etc/ld.so.conf(及其引入的文件)中添加该库的安装目录。

ldconfig工具建立的共享库缓存文件一般为:/etc/ld.so.cache

 

对于一些只有少数特定的程序才会使用的共享库,那么就没有必要加入系统库路径,以免应用程序的共享库之间发生版本冲突。那么,还可以通过设置环境变量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

master

Stay hungry, stay foolish.

One Comment

  1. 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

发表评论

电子邮件地址不会被公开。 必填项已用*标注