【问题标题】:g++ link to shared library not being compiledg ++链接到未编译的共享库
【发布时间】:2014-04-29 18:14:21
【问题描述】:

我在 /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

【问题讨论】:

  • 试试ls -l看看libopenbabel.so.4指向什么
  • @SakthiKumar,谢谢,刚刚将输出添加到问题中

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


【解决方案1】:

要么将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)

【讨论】:

  • 在我的/etc/ld.so.conf 文件中include /etc/ld.so.conf.d/*.conf 行我检查了/etc/ld.so.conf.d 中的文件,发现一个名为libc.conf 的文件包含/usr/local/lib 行所以我认为这个目录已经在由 ld 搜索。
  • 无论出于何种原因,我猜想在 libc.conf 中有 /usr/local/lib 并没有在 /usr/local/lib 中进行编译器搜索。在手动将路径添加到/usr/local/lib 并按照您所说的运行 sudo ldconfig 后,程序就可以工作了。非常感谢您的帮助。
  • 尝试以 root 身份再次运行 ldconfig -v。然后使用ldd
猜你喜欢
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
相关资源
最近更新 更多