2010-07-26 71 views
0

我正在關注使用g ++進行靜態和動態庫鏈接的C++ Cookbook教程。而載入共享庫的錯誤:我可以建立二元罰款,但是當我運行它,我得到的錯誤在加載共享庫時出現g ++ 4.4「錯誤」

./hellobeatles libjohnpaul.so:無法打開共享對象文件:沒有這樣的文件或目錄

我所使用的命令 :克++ -o hellobeatles hellobeatles.cpp -L ../johnpaul/ -L ../georgeringo/ -ljohnpaul -lgeorgeringo

程序構建並運行很好,如果我明確列出像 的路徑:克++ -o hellobeatles hellobeatles.cpp ../johnpaul/libjohnpaul.so ../georgeringo/libgeorgeringo.so

我是否在第一個命令中錯誤地鏈接到libaries?還是有一些配置設置需要處理?

我正在VirtualBox中運行Ubuntu 9.10 guest虛擬機,如果這很重要,並且這裏是-v 使用內置規格。 目標:x86_64-linux-gnu 配置爲:../src/configure -v --with-pkgversion ='Ubuntu 4.4.1-4ubuntu9'--with-bugurl = file:/// usr/share/doc /gcc-4.4/README.Bugs --enable-languages = c,C++,fortran,objc,obj-C++ --prefix =/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir =/usr/lib --without-included-gettext --enable-threads = posix --with-gxx-include-dir =/usr/include/C++/4.4 --program -suffix = -4.4 --enable-nls --enable-clocale = gnu --enable-libstdcxx-debug --enable-objc -gc --disable-werror --with-arch-32 = i486 --with-tune = generic --enable-checking = release --build = x86_64-linux-gnu -host = x86_64-linux-gnu -target = x86_64-linux-gnu 線程模型:posix gcc版本4.4.1(Ubuntu 4.4 .1-4ubuntu9)

回答

2

動態鏈接器e期望在/usr/lib/lib,/usr/local/lib以及可能的其他幾個地方找到共享庫。當然不是../johnpaul/找他們。

如果要將庫安裝到全局位置,請將它們安裝在那裏。

否則,您必須告訴動態鏈接器在哪裏可以找到它們。

更好的方法是將它們添加到編碼成可執行RPATH:

g++ -o hellobeatles hellobeatles.cpp \ 
    -L ../johnpaul/ -L ../georgeringo/ -ljohnpaul -lgeorgeringo \ 
    -Wl,-rpath=/path/to/johnpaul:/path/to/georgeringo 

替代(和較不優選的)的方法是:

export LD_LIBRARY_PATH=/path/to/johnpaul:/path/to/georgeringo