Linux中的动态库

时间:2014-01-02 00:18:13

标签: linux gcc

当我们使用动态库创建二进制文件时,我们应用gcc -o binary main.o -L. -lmylib -Wl,-rpath,.,其中-L.表示链接器必须在当前目录中搜索库。为什么没有-Wl,-rpath,.我们无法使用动态库?

2 个答案:

答案 0 :(得分:0)

默认情况下,ld.so会在LD_LIBRARY_PATH指定的目录中进行搜索。如果您的共享库不属于其中一个,则无法找到它。

-rpath的{​​{1}}选项使其在ld将查看的可执行文件中存储路径名。

答案 1 :(得分:0)

系统中有几个目录,其中ld.so查找库。如果您的库不在该列表的目录中,您有两种选择:

  • LD_LIBRARY_PATH变量中指定其路径;
  • 在二进制文件的rpath中写入路径。

您可以使用chrpath更改/查看二进制文件的路径。

-rpath-L之间的差异:

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

VS

-L searchdir
--library-path=searchdir
      Add path searchdir to the list of paths that ld will search for
      archive libraries and ld control scripts.

What's the difference between -rpath and -L?

相关问题