无法使rpath正常工作并使二进制文件在指定文件夹中搜索库:
我有三个非常简单的文件:
main.c
#include <stdio.h>
#include <func.h>
int main() {
testing();
return 1;
}函数.h
void testing();函数.c
#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文件(即)时才会在指定的路径中搜索库。
我做错了什么?
编辑:在接受了答案之后,我在这里留下了使用它的确切行,并让它对任何可能发现它有用的人都有用:
gcc main.c -L/home/root -Wl,-rpath,'/home/root/' -l:libtime_mgmt -o ${OUT_FILE}注意: rpath是与简单的路径一起使用的。不知道这是否是它以前不起作用的原因,但它现在是这样运作的。
发布于 2018-02-12 15:38:12
rpath不是在编译时使用,而是在链接/运行时使用.因此,您可能需要同时使用这两种方法:
-L /home/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 "..."的使用是有问题的。请参阅: and #include "filename"?
https://stackoverflow.com/questions/48749213
复制相似问题