![](/img/trans.png)
[英]adding “-rpath,/usr/lib” in the build options of a shared library cause a segfault
[英]Shared library and rpath
我无法使rpath正常工作并使我的二进制文件在指定文件夹中搜索该库:
我有3个非常简单的文件:
main.c
#include <stdio.h>
#include <func.h>
int main() {
testing();
return 1;
}
func.h
void testing();
函数
#include "func.h"
void testing(){
printf(testing\n");
}
然后我继续创建一个共享库 ,如下所示:
gcc -c -fpic func.c -o ../release/func.o
gcc -shared -o ../release/lib/lib_func.so ../release/func.o
然后编译程序 :
gcc main.c ../release/lib/lib_time_mgmt.so -Wl,-rpath=/home/root/ -o ../release/main
我收到下一个警告:
main.c:7:2: warning: implicit declaration of function ‘testing’ [-Wimplicit-function-declaration]
testing();
但是除此之外,该程序还可以正常运行。
但是,我的问题是,如果现在我想将库移至/ home / root(如rpath中所指定),它将无法工作,并且仅在编译main.c文件时指定的路径中仍会搜索该库,即../release/lib/lib_time_mgmt.so
我究竟做错了什么?
编辑:接受答案后,我在这里使用它的确切行,并使其对可能有用的人起作用:
gcc main.c -L/home/root -Wl,-rpath,'/home/root/' -l:libtime_mgmt -o ${OUT_FILE}
注意:rpath与简单'之间的路径一起使用。 不知道这是否是以前不起作用的原因,但是现在它是这样工作的。
rpath
不是在编译时使用的,而是在链接/运行时使用的...因此,您可能需要同时使用这两个方法:
-L /home/root
root-在构建时正确链接 -Wl,-rpath=/home/root
在运行时正确链接 您应该使用-l ${lib}
标志来链接库,不要将它们的路径指定为输入。
除此之外,约定指出这些库被命名为libNAME.so
例如:
-l func
将尝试与libfunc.so
链接 -l time_mgmt
将尝试与libtime_mgmt.so
链接 解决以上问题后,请尝试以下操作:
gcc main.c -L/home/root -Wl,-rpath=/home/root -lfunc -ltime_mgmt -o ${OUT_FILE}
最后一点,我建议您不要使用rpath
,而应着重在正确的位置安装库。
与您的问题无关,但值得注意。 您对#include <...>
与#include "..."
的使用值得怀疑。 请参阅: #include <文件名>和#include“文件名”有什么区别?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.