簡體   English   中英

了解Linux中共享庫的名稱

[英]Understanding names of shared library in linux

我已經閱讀了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

但這是行不通的。 導致以下錯誤:/ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM