Linker 与rpath的重新连接

Linker 与rpath的重新连接,linker,shared-libraries,portability,ld,rpath,Linker,Shared Libraries,Portability,Ld,Rpath,我想创建一个可执行文件(exec),它链接一个动态库(shared2),该动态库链接另一个共享库(shared1),这样我就不必在exec的makefile中指定shared1。具体地说,shared2将-rpath指定给shared1,但当我编译exec时,rpath是相对于exec而不是shared2计算的。这要求我在exec内部指定shared1的-rpath(这是我想要避免的) 以下玩具示例说明了我的观点: 目录树是: Exec main.cpp makefile sha

我想创建一个可执行文件(exec),它链接一个动态库(shared2),该动态库链接另一个共享库(shared1),这样我就不必在exec的makefile中指定shared1。具体地说,shared2将-rpath指定给shared1,但当我编译exec时,rpath是相对于exec而不是shared2计算的。这要求我在exec内部指定shared1的-rpath(这是我想要避免的)

以下玩具示例说明了我的观点:

目录树是:

Exec
    main.cpp
    makefile
shared2
    shared2
        shared2.cpp
        shared2.h
        makefile
shared1
    shared1.cpp
    shared1.h
    makefile
Exec/makefile

app: main.o
    g++ main.o -o app -L../shared2/shared2 -Wl,-rpath,../shared2/shared2 -lshared2 #-Wl,-rpath,../shared1

main.o: main.cpp
    g++ -g -c -o main.o main.cpp -I../shared2/shared2

clean:
    rm -f app main.o
shared2/shared2/makefile

libshared2.so: shared2.o
    g++ -shared shared2.o -o libshared2.so -L../../shared1 -Wl,-rpath,../../shared1 -lshared1 

shared2.o: shared2.cpp
    g++ -fPIC -g -c -o shared2.o shared2.cpp -I../../shared1

clean:
    rm -f libshared2.so shared2.o
shared1/makefile

libshared1.so: shared1.o
    g++ -shared shared1.o -o libshared1.so

shared1.o: shared1.cpp
    g++ -fPIC -g -c -o shared1.o shared1.cpp

clean:
    rm -f libshared1.so shared1.o
我让main.cpp使用shared2.cpp中的内容,让shared2.cpp中的内容使用shared1.cpp中的内容

当我去编译Exec/makefile时,shared2/shared2/makefile中指定的rpath是相对于Exec/makefile的位置计算的,因此编译失败。请注意,我在第一个makefile中引用的一点是它成功的必要条件,但我希望避免这种情况

有解决办法吗


非常感谢:)

如果您关心Linux或Solaris,请更改以下内容:

libshared2.so: shared2.o
    g++ -shared shared2.o -o libshared2.so -L../../shared1 \
        -Wl,-rpath,../../shared1 -lshared1
为此:

libshared2.so: shared2.o
    g++ -shared shared2.o -o libshared2.so -L../../shared1 \
       -Wl,-rpath,'$$ORIGIN/../../shared1' -lshared1

注意:
$$ORIGIN
周围的单引号是必需的。

亲爱的俄罗斯人,谢谢你的回答。当我按照你的建议去做时,当它编译exec时,我得到/usr/bin/ld:warning:libshared1.so,被../shared2/shared2/libshared2.so所需要,找不到(尝试使用-rpath或-rpath链接)…/shared2/shared2/libshared2.so:func1()集合的未定义引用2:error:ld返回了1个退出状态我正在运行Ubuntu 12.04,g++4.8 ldd libshared2.so?@megavore请注意,答案假设您编辑
Makefile
。如果直接执行命令,则需要将
$$ORIGIN
替换为
$ORIGIN
(单个
$
符号)。