了解linux中共享库的名称

时间:2014-04-24 04:43:40

标签: c++ linux shared-libraries

我已经阅读了linux文档项目,但我仍然不了解sonamereal name的确切目的。

我要提供三个文件:main.cppheader.htest.cpp。我写了以下Makefile:

bin: main.o test.so
    g++ -o bin main.o -ltest  -Wl,-rpath /usr/local/lib
main.o:main.cpp
    g++ -c main.cpp
test.so: test.o
    g++ -shared -o libtest.so test.o
test.o: test.cpp
    g++ -fPIC -c test.cpp
clean:
    rm -f *.o *.so* bin

我已经运行了这个make文件,我已经将libtest.so复制到/usr/local/lib并且没问题。但我不明白我的sonamereal name是什么意思?

让我们重写这个Makefile:

bin: main.o test.so
    g++ -o bin main.o -L. -ltest  -Wl,-rpath .
main.o:main.cpp
    g++ -c main.cpp
test.so: test.o
    g++ -shared -Wl,soname,-litest.so.1 -o libtest.so.1.0.1 test.o
test.o: test.cpp
    g++ -fPIC -c test.cpp
clean:
    rm -f *.o *.so* bin

但它不起作用。导致以下错误:

/usr/bin/ld: cannot find -ltest
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:0)

  

但它不起作用。导致以下错误:/ usr / bin / ld:找不到-ltest

您似乎使用了错误的选项。

1)-rpath用于运行时库搜索路径。所有-rpath参数都连接在一起并传递给运行时链接器ld.so请参阅man ld.so

2)-L用于链接您的程序或共享库时的ld(来自man ld:所有-L选项适用于所有-l选项)

因此,在构建时,您需要使用-L告诉链接器ld在哪里找到库(并记住 - 启动程序时使用ld.so,链接时ld它):

g++ -o bin main.o -L. -ltest  -Wl,-rpath /usr/local/lib
相关问题