- rpath 和-L 有什么区别?

GCC (aka gcc)和 ld 提供了许多方法来指定库的搜索路径,其中包括 -rpath-L标志。手册显示这两个标志之间没有区别,实际上是说每个标志在图书馆搜索路径中添加了一个库。然而奇怪的是,这两个标志做 没错相同的事情。如果有的话,这两种选择有什么不同?

64353 次浏览

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.

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.

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.