2010-07-26 91 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