问题描述
我必须将我的代码链接到不带 lib 前缀的共享库.(比如 foo.so) 第一个问题是 -l 选项找不到文件.所以我尝试直接将这个文件包含到最后的编译中,如下所示:
I have to link my code to a shared library without the lib prefix. (say, foo.so) The first problem is -l option does not find the file. So I tried directly including this file to the last compilation like this:
gcc a.o/PATH/TO/FOO/foo.so
gcc a a.o /PATH/TO/FOO/foo.so
但在这种情况下,a 硬链接到 foo.so 作为绝对路径,如ldd a"中所示:
But in this case, a is hard linked to foo.so as an absolute path as seen in "ldd a":
/PATH/TO/FOO/foo.so
/PATH/TO/FOO/foo.so
在最终部署中,两个文件最终会位于同一个文件夹中,因此这应该是正常链接,而不是绝对路径.我该怎么做?
In the final deployment both files would end up being in the same folder, so this should be normal link, not the absolute path. How can I do this?
推荐答案
-Wl,-rpath,.--> 使用当前目录搜索 lib 文件.(即使在编译中没有找到,在运行时也可以)而不是 -llibrary --> 使用 library.so.
-Wl,-rpath,. --> to use current directory for searching lib files. (even if not found in compilation, ok at run-time)instead of -llibrary --> use library.so.
这似乎工作正常.希望任何人都觉得这很有用.
This seems to work correctly. Hope anyone finds this useful.
这篇关于如何链接到不同目录中没有 lib* 前缀的共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!