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(-R): “运行”的时候,去找的目录。对于交叉编译,交叉编译链接器需已经配置 –with-sysroot 选项才能起作用

7. ld链接选项

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

# gcc -Wl,--start-group foo.o bar.o -Wl,--end-group
# gcc obs.c -leSDKOBSS3 -Wl,-R,/root/hbu8.0/bin/lib
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值