Lib依赖库

1. 查看依赖库

# ldd xxx

当出现如下错误:error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决办法:

  1. 如果是ubuntu,ld认的目录是/lib和/usr/lib,不会查找/usr/lib64,需要手动添加。redhat会自动查找
  2. 先用查找命令在相应的lib目录下查找,是否此库文件存在
  3. 修改 /etc/ld.so.conf 文件,将路径添加到最后即可
  4. 或者在 /etc/ld.so.conf.d/ 下新建一文件,如 XXX.conf,其内容还是库路径
  5. # sudo ldconfig

注:每次添加新的库文件时,都需要重新生效

2. 如果找不到

可能是库的权限问题,普通用户用不了

3. 如果还找不到

说明64位系统和32的支持

# sudo apt-get install ia32-libs 

4. rpath

man ld,查找链接选项rpath的含义:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-rpath=dir
Add a directory to the runtime library search path. This is used when
linking an ELF executable with shared objects. All -rpath arguments
are concatenated and passed to the runtime linker,which uses them to
locate shared objects at runtime. The -rpath option is also used when
locating shared objects which are needed by shared objects explicitly
included in the link; see the description of the -rpath-link option.
If -rpath is not used when linking an ELF executable,the contents of
the environment variable “LD_RUN_PATH” will be used if it is defined.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
简单翻译下,rpath链接选项主要有两个功能

  1. 程序运行时,优先到rpath指定的目录去寻找依赖库
  2. 程序链接时,在指定的目录中,隐式的链接那些动态库所需要的链接

5. 动态链接的优先级

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.
  1. linux的搜索路径是/lib和/usr/lib,然后按照/etc/ld.so.conf里面的配置搜索绝对路径
  2. LD_LIBRARY_PATH路径优先于系统认路径之前查找
  3. gcc的-R或-rpath选项来在编译时就指定库的查找路径,并且该库的路径信息保存在可执行文件中,运行时它会直接到该路径查找库

6. 动态库的链接

  1. -L: “链接”的时候,去找的目录。都会先从 -L 指定的目录去找,然后是认的地方。-L只是指定了程序编译连接时库的路径,并不影响程序执行时库的路径,系统还是会到认路径下查找该程序所需要的库
  2. -rpath-link:“链接”的时候,去找的目录。
  3. -rpath: “运行”的时候,去找的目录。对于交叉编译,交叉编译链接器需已经配置 –with-sysroot 选项才能起作用。

7. ld链接选项

在gcc中使用ld链接选项时,需要在选项前面加上前缀-Wl(是字母l,不是1),以区别不是编译器的选项

# gcc -Wl,--start-group foo.o bar.o -Wl,--end-group

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...