LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1166|回复: 4

如果一个程序运行时需要链接库,它如何找到那个库文件?

[复制链接]
发表于 2008-2-21 01:46:38 | 显示全部楼层 |阅读模式
就像系统自带程序运行时在$PAH中搜索一样,搜索库也是有个指定的路径吗?
发表于 2008-2-21 11:42:32 | 显示全部楼层
那是当然的咯,系统默认会在/usr/lib下找库文件,如果是其他地方那么就需要在/etc/ld.so.conf文件里添加路径告诉操作系统,不要忘记在更新了/etc/ld.so.conf后要执行命令ldconfig
回复 支持 反对

使用道具 举报

发表于 2008-2-21 12:42:04 | 显示全部楼层
除了 2 楼所说的方法外, 也可以通过环境变量或给 /lib/ld-linux.so.2 传参数来在运行时临时改变查找路径. 看下面几个命令

  1. [yun@localhost ~]$ echo "hello" # 这是默认查找路径下的结果, 一切正常
  2. hello


  3. [yun@localhost ~]$ /lib/ld-linux.so.2 # 对, 就是要直接运行
  4. Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
  5. You have invoked `ld.so', the helper program for shared library executables.
  6. This program usually lives in the file `/lib/ld.so', and special directives
  7. in executable files using ELF shared libraries tell the system's program
  8. loader to load the helper program from this file.  This helper program loads
  9. the shared libraries needed by the program executable, prepares the program
  10. to run, and runs it.  You may invoke this helper program directly from the
  11. command line to load and run an ELF executable file; this is like executing
  12. that file itself, but always uses this helper program from the file you
  13. specified, instead of the helper program file specified in the executable
  14. file you run.  This is mostly of use for maintainers to test new versions
  15. of this helper program; chances are you did not intend to run this program.

  16.   --list                list all dependencies and how they are resolved
  17.   --verify              verify that given object really is a dynamically linked
  18.                         object we can handle
  19.   --library-path PATH   use given PATH instead of content of the environment
  20.                         variable LD_LIBRARY_PATH
  21.   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
  22.                         in LIST
  23. [yun@localhost ~]$ /lib/ld-linux.so.2 --library-path / echo "hello" # 指定查找路径 (只) 为 "/", 因此会找不到库而无法运行
  24. echo: error while loading shared libraries: echo: cannot open shared object file
复制代码


更具体的请参见 man ld.so
回复 支持 反对

使用道具 举报

发表于 2008-2-21 17:03:46 | 显示全部楼层
编译的时候告诉  库 在哪里可以吗?
回复 支持 反对

使用道具 举报

发表于 2008-2-26 14:18:49 | 显示全部楼层
$LD_LIBRARY_PATH
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表