176

我正在尝试在此 Debian lenny 系统上将应用程序与 g++ 链接。ld 抱怨找不到指定的库。这里的具体示例是 ImageMagick,但我在其他一些库中也遇到了类似的问题。

我用以下方式调用链接器:

g++ -w (..lots of .o files/include directories/etc..) \
-L/usr/lib -lmagic

ld抱怨:

/usr/bin/ld: cannot find -lmagic

但是,libmagic 存在:

$ locate libmagic.so
/usr/lib/libmagic.so.1
/usr/lib/libmagic.so.1.0.0
$ ls -all /usr/lib/libmagic.so.1*
lrwxrwxrwx 1 root root    17 2008-12-01 03:52 /usr/lib/libmagic.so.1 -> libmagic.so.1.0.0
-rwxrwxrwx 1 root root 84664 2008-09-09 00:05 /usr/lib/libmagic.so.1.0.0
$ ldd /usr/lib/libmagic.so.1.0.0 
    linux-gate.so.1 =>  (0xb7f85000)
    libz.so.1 => /usr/lib/libz.so.1 (0xb7f51000)
    libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7df6000)
    /lib/ld-linux.so.2 (0xb7f86000)
$ sudo ldconfig -v | grep "libmagic"
    libmagic.so.1 -> libmagic.so.1.0.0

我如何进一步诊断这个问题,可能出了什么问题?我在做一些完全愚蠢的事情吗?

4

8 回答 8

178

问题是链接器正在寻找libmagic.so但你只有libmagic.so.1

一个快速的技巧是符号链接libmagic.so.1libmagic.so

于 2008-12-03T01:06:18.170 回答
70

正如 grepsedawk 刚刚制定的那样,答案在于-l选择g++, 调用ld。如果您查看此命令的手册页,您可以执行以下操作:

  • g++ -l:libmagic.so.1 [...]
  • 或:g++ -lmagic [...],如果您的库路径中有一个名为 libmagic.so 的符号链接
于 2008-12-03T01:14:07.267 回答
34
于 2010-10-27T05:17:10.963 回答
9

In Ubuntu, you can install libtool which resolves the libraries automatically.

$ sudo apt-get install libtool

This resolved a problem with ltdl for me, which had been installed as libltdl.so.7 and wasn't found as simply -lltdl in the make.

于 2012-05-23T09:10:56.697 回答
6

As mentioned above the linker is looking for libmagic.so, but you only have libmagic.so.1.

To solve this problem just perform an update cache.

ldconfig -v 

To verify you can run:

$ ldconfig -p | grep libmagic
于 2016-05-17T20:51:24.983 回答
4

除非我弄错了libmagic或者-lmagic与 ImageMagick 不是同一个库。你声明你想要 ImageMagick。

ImageMagick 带有一个实用程序,可为编译器提供所有适当的选项。

前任:

g++ program.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs` -o "prog"
于 2008-12-03T01:08:20.507 回答
1

Installing libgl1-mesa-dev from the Ubuntu repo resolved this problem for me.

于 2014-07-01T21:25:31.343 回答
0

I tried all solutions mentioned above but none of them solved my issue but finally I solved it with the following command.

sudo apt-get install libgmp3-dev

This will do the magic.

于 2021-07-07T06:53:42.297 回答