c++ - 共享库的 g++ 链接未编译

标签 c++ g++ shared-libraries shared openbabel

我在/usr/local/lib 中有这些共享库文件:

libopenbabel.so
libopenbabel.so.4
libopenbabel.so.4.0.2

libopenbabel.so 实际上是 libopenbabel.so.4 的链接,libopenbabel.so.4 实际上是 libopenbabel.so.4.0.2 的链接

当我使用此命令编译文件时:

g++ test.cpp -L/usr/local/lib -lopenbabel

然后尝试运行 a.out,我收到此错误:

./a.out: error while loading shared libraries: libopenbabel.so.4: cannot open shared object file: No such file or directory

这意味着编译器正在查找 libopenbabel.so,但在跟踪位于完全相同目录中的 libopenbabel.so.4 的链接时遇到错误。关于为什么会发生这种情况/如何解决它有什么想法吗?

/usr/local/lib 中 ls -l 的输出:

total 33772
drwxr-xr-x  7 root root      4096 Mar 22 01:24 .
drwxr-xr-x 10 root root      4096 Aug 20  2013 ..
drwxr-xr-x  3 root root      4096 Mar 21 23:45 cmake
lrwxrwxrwx  1 root root        13 Mar 21 23:45 libinchi.so -> libinchi.so.0
lrwxrwxrwx  1 root root        17 Mar 21 23:45 libinchi.so.0 -> libinchi.so.0.4.1
-rw-r--r--  1 root root   3315565 Mar 22 01:04 libinchi.so.0.4.1
lrwxrwxrwx  1 root root        17 Mar 21 23:45 libopenbabel.so -> libopenbabel.so.4
lrwxrwxrwx  1 root root        21 Mar 21 23:45 libopenbabel.so.4 -> libopenbabel.so.4.0.2
-rw-r--r--  1 root root  31232420 Mar 22 01:04 libopenbabel.so.4.0.2
drwxr-xr-x  3 root root      4096 Mar 21 23:45 openbabel
drwxr-xr-x  2 root root      4096 Mar 22 01:24 pkgconfig
drwxrwsr-x  4 root staff     4096 Mar  3 12:57 python2.7
drwxrwsr-x  3 root staff     4096 Dec 22 18:25 python3.2

最佳答案

设置 LD_LIBRARY_PATH 环境变量以包含 /usr/local/lib 或将 /usr/local/lib 添加到 /etc/ld.so.conf 并以 root 身份运行 ldconfig

此外,运行ldd a.out来检查动态链接在您的应用上是否正常工作。

阅读ld-linux(8) , ldconfig(8) , ldd(1)

关于c++ - 共享库的 g++ 链接未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22574677/

相关文章:

c++ - 为什么我的 dev-c++ "can' t 打开输出文件”?

python - 仅当树莓派要求时才在 Arduino 上运行 C++

c++ - extern "C"用法(标记的放置)

c++ - 了解 reinterpret_cast

c++ - ubuntu 上的 g++ mudflap

c++ - 你如何找到你的 linux 机器上安装了哪个版本的 libstdc++ 库?

c++ - 如何在 Linux 上延迟加载共享库

jenkins - 如何静态检查使用共享库的 Jenkins 管道的代码?

c++ - 什么是 friend ostream

c++ - 智能指针模板