LD的-rpath,-rpath-link

http://blog.chinaunix.net/uid-24709751-id-3563351.htmlphp

http://songzhangzhang.blog.163.com/blog/static/694019812011413241852/
http://bbs.et8.net/bbs/showthread.php?t=1018453
-rpath=dirhtml

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-link=dirapp

When using ELF or SunOS, one shared library may require another. This happens when an ld -shared link includes a shared library as one of the input files. When the linker encounters such a dependency when doing a non-shared, non-relocatable link, it will automatically try to locate the required shared library and include it in the link, if it is not included explicitly. In such a case, the -rpath-link option specifies the first set of directories to search. The -rpath-link option may specify a sequence of directory names either by specifying a list of names separated by colons, or by appearing multiple times. 
This option should be used with caution as it overrides the search path that may have been hard compiled into a shared library. In such a case it is possible to use unintentionally a different search path than the runtime linker would do.


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.




-L: 「連接」的時候,去找的目錄,也就是全部的 -lFOO 選項裏的庫,都會先從 -L 指定的目錄去找,而後是默認的地方。
-rpath: 「運行」的時候,去找的目錄。運行的時候,要找 .so 文件,會從這個選項裏指定的地方去找。對於交叉編譯,只有配合 --sysroot 選項才能起做用。
-rpath_link (或者 -rpath-link):這個也是用於「連接」的時候的,例如你顯示指定的須要 FOO.so,可是 FOO.so 自己是須要 BAR.so 的,後者你並無指定,而是 FOO.so 引用到它,這個時候,會先從 -rpath-link 給的路徑裏找。

也就是說,-rpath指定的路徑會被記錄在生成的可執行程序中,用於運行時。
-rpath-link 則只用於連接時。ide

相關文章
相關標籤/搜索