简体   繁体   English

共享库和rpath

[英]Shared library and rpath

I cannot make rpath work properly and make my binary to search for the library in the specified folder: 我无法使rpath正常工作并使我的二进制文件在指定文件夹中搜索该库:

I have 3 very simple files: 我有3个非常简单的文件:

main.c main.c

#include <stdio.h>
#include <func.h>
int main() {
    testing();
return 1;
}

func.h func.h

void testing();

func.c 函数

#include "func.h"
void testing(){

    printf(testing\n");
}

Then I proceed to create a shared library as it follows: 然后我继续创建一个共享库 ,如下所示:

gcc -c -fpic func.c -o ../release/func.o
gcc -shared -o ../release/lib/lib_func.so ../release/func.o

And then compile the program : 然后编译程序

gcc main.c ../release/lib/lib_time_mgmt.so -Wl,-rpath=/home/root/ -o ../release/main

I receive the next warning: 我收到下一个警告:

main.c:7:2: warning: implicit declaration of function ‘testing’ [-Wimplicit-function-declaration]
testing();

But besides it, the program works fine. 但是除此之外,该程序还可以正常运行。

However, my problem is that if now I want to move the library to /home/root (as specified in rpath ) it does not work and the library is still searched only in the path specified when I compiled the main.c file which is ../release/lib/lib_time_mgmt.so 但是,我的问题是,如果现在我想将库移至/ home / root(如rpath中所指定),它将无法工作,并且仅在编译main.c文件时指定的路径中仍会搜索该库,即../release/lib/lib_time_mgmt.so

What am I doing wrong? 我究竟做错了什么?


EDIT: After accepting the answer, I leave here the exact line as I used it and made it work for whoever might find it useful: 编辑:接受答案后,我在这里使用它的确切行,并使其对可能有用的人起作用:

gcc main.c -L/home/root -Wl,-rpath,'/home/root/' -l:libtime_mgmt -o ${OUT_FILE}

Note: the rpath was used with the path betwen simple '. 注意:rpath与简单'之间的路径一起使用。 Not sure if that was the reason why it was not working before, but it worked this way now. 不知道这是否是以前不起作用的原因,但是现在它是这样工作的。

rpath is not used at compile time, but rather at link/runtime... thus you probably need to use both of these: rpath不是在编译时使用的,而是在链接/运行时使用的...因此,您可能需要同时使用这两个方法:

  • -L /home/root - to link correctly at build time -L /home/root root-在构建时正确链接
  • -Wl,-rpath=/home/root - to link correctly at run-time -Wl,-rpath=/home/root在运行时正确链接

You should use the -l ${lib} flag to link with libraries, don't specify their path as an input. 您应该使用-l ${lib}标志来链接库,不要将它们的路径指定为输入。

In addition to this, convention states that the libraries are named libNAME.so - eg: 除此之外,约定指出这些库被命名为libNAME.so例如:

  • -l func will try to link with libfunc.so -l func将尝试与libfunc.so链接
  • -l time_mgmt will try to link with libtime_mgmt.so -l time_mgmt将尝试与libtime_mgmt.so链接

Once you've addressed the above points, try the following: 解决以上问题后,请尝试以下操作:

gcc main.c -L/home/root -Wl,-rpath=/home/root -lfunc -ltime_mgmt -o ${OUT_FILE}

As a final point, I'd advise that you try not to use rpath , and instead focus on installing libraries in the correct places. 最后一点,我建议您不要使用rpath ,而应着重在正确的位置安装库。


Unrelated to your question, but worth noting. 与您的问题无关,但值得注意。 Your use of #include <...> vs #include "..." is questionable. 您对#include <...>#include "..."的使用值得怀疑。 See: What is the difference between #include <filename> and #include "filename"? 请参阅: #include <文件名>和#include“文件名”有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM