1. 查看依赖库
# ldd xxx
当出现如下错误:error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决办法:
- 如果是ubuntu,ld默认的目录是/lib和/usr/lib,不会查找/usr/lib64,需要手动添加。redhat会自动查找
- 先用查找命令在相应的lib目录下查找,是否此库文件存在
- 修改 /etc/ld.so.conf 文件,将路径添加到最后即可
- 或者在 /etc/ld.so.conf.d/ 下新建一文件,如 XXX.conf,其内容还是库路径
- # 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链接选项主要有两个功能:
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.
- linux的默认搜索路径是/lib和/usr/lib,然后按照/etc/ld.so.conf里面的配置搜索绝对路径
- LD_LIBRARY_PATH路径优先于系统默认路径之前查找
- gcc的-R或-rpath选项来在编译时就指定库的查找路径,并且该库的路径信息保存在可执行文件中,运行时它会直接到该路径查找库
6. 动态库的链接
- -L: “链接”的时候,去找的目录。都会先从 -L 指定的目录去找,然后是默认的地方。-L只是指定了程序编译连接时库的路径,并不影响程序执行时库的路径,系统还是会到默认路径下查找该程序所需要的库
- -rpath-link:“链接”的时候,去找的目录。
- -rpath: “运行”的时候,去找的目录。对于交叉编译,交叉编译链接器需已经配置 –with-sysroot 选项才能起作用。
7. ld链接选项
在gcc中使用ld链接选项时,需要在选项前面加上前缀-Wl(是字母l,不是1),以区别不是编译器的选项
# gcc -Wl,--start-group foo.o bar.o -Wl,--end-group