-rpath和-L的区别是什么?

时间:2022-02-17 04:50:32

gcc and ld provide many ways to specify a search path for libraries—among them the -rpath and -L flags. The manpages reveal no differences between these two flags, effectively saying each flag adds a library to the library search path. Yet it seems strange that both flags do exactly the same thing. What are the differences, if any, between these two options?

gcc和ld提供了许多方法来指定图书馆员的搜索路径,其中包括-rpath和-L标志。manpages没有显示这两种标志之间的差异,实际上是说每个标志都将一个库添加到库搜索路径中。然而,似乎奇怪的是,这两种旗帜都做着同样的事情。这两种选择之间有什么区别?

1 个解决方案

#1


69  

You must be reading some outdated copies of the manpages (emphasis added):

你必须阅读一些过时的手册(重点补充):

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

-rpath=dir将一个目录添加到运行库搜索路径。这是在将一个ELF可执行文件与共享对象链接时使用的。所有的-rpath参数被连接并传递给运行时链接器,它使用它们在运行时定位共享对象。

vs.

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.

-L searchdir - libre -path=searchdir添加路径searchdir到ld将搜索存档库和ld控制脚本的路径列表。

So, -L tells ld where to look for libraries to link against when linking. You use this (for example) when you're building against libraries in your build tree, which will be put in the normal system library paths by make install. --rpath, on the other hand, stores that path inside the executable, so that the runtime dynamic linker can find the libraries. You use this when your libraries are outside the system library search path.

所以,-L告诉ld在哪里查找与链接时相链接的库。当您在构建树中对库进行构建时,您将使用这个(例如),它将被安装在正常的系统库路径中。另一方面,rpath将路径存储在可执行文件内,这样运行时动态链接器就可以找到这些库。当您的库位于系统库搜索路径之外时,您将使用此方法。

#1


69  

You must be reading some outdated copies of the manpages (emphasis added):

你必须阅读一些过时的手册(重点补充):

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

-rpath=dir将一个目录添加到运行库搜索路径。这是在将一个ELF可执行文件与共享对象链接时使用的。所有的-rpath参数被连接并传递给运行时链接器,它使用它们在运行时定位共享对象。

vs.

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.

-L searchdir - libre -path=searchdir添加路径searchdir到ld将搜索存档库和ld控制脚本的路径列表。

So, -L tells ld where to look for libraries to link against when linking. You use this (for example) when you're building against libraries in your build tree, which will be put in the normal system library paths by make install. --rpath, on the other hand, stores that path inside the executable, so that the runtime dynamic linker can find the libraries. You use this when your libraries are outside the system library search path.

所以,-L告诉ld在哪里查找与链接时相链接的库。当您在构建树中对库进行构建时,您将使用这个(例如),它将被安装在正常的系统库路径中。另一方面,rpath将路径存储在可执行文件内,这样运行时动态链接器就可以找到这些库。当您的库位于系统库搜索路径之外时,您将使用此方法。