ld 找不到现有的库
我正在尝试在这个 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
如何进一步诊断此问题,可能出了什么问题? 我在做一些完全愚蠢的事情吗?
I am attempting to link an application with g++ on this Debian lenny system. ld is complaining it cannot find specified libraries. The specific example here is ImageMagick, but I am having similar problems with a few other libraries too.
I am calling the linker with:
g++ -w (..lots of .o files/include directories/etc..) \
-L/usr/lib -lmagic
ld complains:
/usr/bin/ld: cannot find -lmagic
However, libmagic exists:
$ 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
How do I diagnose this problem further, and what could be wrong? Am I doing something completely stupid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
问题是链接器正在寻找
libmagic.so
但你只有libmagic.so.1
一个快速的技巧是符号链接
libmagic.so.1
代码>到libmagic.so
The problem is the linker is looking for
libmagic.so
but you only havelibmagic.so.1
A quick hack is to symlink
libmagic.so.1
tolibmagic.so
正如grepsedawk刚刚表述的那样,答案在于
g++
的-l
选项,调用ld
。 如果您查看此命令的手册页,您可以执行以下操作:g++ -l:libmagic.so.1 [...]
g++ -lmagic [...]
,如果您的库路径中有一个名为 libmagic.so 的符号链接As just formulated by grepsedawk, the answer lies in the
-l
option ofg++
, callingld
. If you look at the man page of this command, you can either do:g++ -l:libmagic.so.1 [...]
g++ -lmagic [...]
, if you have a symlink named libmagic.so in your libs pathDebian 约定将共享库分为运行时组件 (
libmagic1: /usr/lib/libmagic.so.1 → libmagic.so.1.0.0
) 及其开发组件 (libmagic) -dev: /usr/lib/libmagic.so → ...
)。因为库的soname 是
libmagic.so.1
,所以它是嵌入到可执行文件中的字符串,因此这是运行可执行文件时加载的文件。但是,由于该库被指定为链接器的
-lmagic
,因此它会查找libmagic.so
,这就是开发需要它的原因。请参阅 Diego E. Pettenò:链接器和名称,了解这一切在 Linux 上如何工作的详细信息。
简而言之,您应该
apt-get install libmagic-dev
。 这不仅会为您提供libmagic.so
,还会提供编译所需的其他文件,例如/usr/include/magic.h
。It is Debian convention to separate shared libraries into their runtime components (
libmagic1: /usr/lib/libmagic.so.1 → libmagic.so.1.0.0
) and their development components (libmagic-dev: /usr/lib/libmagic.so → …
).Because the library's soname is
libmagic.so.1
, that's the string that gets embedded into the executable so that's the file that is loaded when the executable is run.However, because the library is specified as
-lmagic
to the linker, it looks forlibmagic.so
, which is why it is needed for development.See Diego E. Pettenò: Linkers and names for details on how this all works on Linux.
In short, you should
apt-get install libmagic-dev
. This will not only give youlibmagic.so
but also other files necessary for compiling like/usr/include/magic.h
.在 Ubuntu 中,您可以安装 libtool 来自动解析库。
这为我解决了
ltdl
的问题,它已作为libltdl.so.7
安装,并且在以下位置中找不到简单的-lltdl
品牌。In Ubuntu, you can install
libtool
which resolves the libraries automatically.This resolved a problem with
ltdl
for me, which had been installed aslibltdl.so.7
and wasn't found as simply-lltdl
in the make.如上所述,链接器正在寻找
libmagic.so
,但您只有libmagic.so.1
。要解决这个问题,只需执行更新缓存即可。
要验证您可以运行:
As mentioned above the linker is looking for
libmagic.so
, but you only havelibmagic.so.1
.To solve this problem just perform an update cache.
To verify you can run:
除非我严重错误
libmagic
或-lmagic
与 ImageMagick 不是同一个库。 您声明您想要 ImageMagick。ImageMagick 附带了一个实用程序,可为编译器提供所有适当的选项。
前任:
Unless I'm badly mistaken
libmagic
or-lmagic
is not the same library as ImageMagick. You state that you want ImageMagick.ImageMagick comes with a utility to supply all appropriate options to the compiler.
Ex:
从 Ubuntu 存储库安装 libgl1-mesa-dev 为我解决了这个问题。
Installing libgl1-mesa-dev from the Ubuntu repo resolved this problem for me.
我尝试了上面提到的所有解决方案,但没有一个解决了我的问题,但最后我用以下命令解决了它。
这会发挥魔法。
I tried all solutions mentioned above but none of them solved my issue but finally I solved it with the following command.
This will do the magic.
解决此问题的另一种方法是安装
-devel
包。如果编译器正在寻找
libabc.so
,而您有libabc.so.1
,则需要安装-devel
软件包,例如libabc-devel
aslibabc.so.1
是一个运行时库,但libabc.so
是一个开发库。Another way to solve this problem is to install the
-devel
package.If the compiler is looking for
libabc.so
while you havelibabc.so.1
, you need to install the-devel
package likelibabc-devel
aslibabc.so.1
is a runtime lib butlibabc.so
is a development lib.