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