Linux ld 找不到现有的库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/335928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ld cannot find an existing library
提问by maxpenguin
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.
我试图在这个 Debian lenny 系统上将应用程序与 g++ 链接起来。ld 抱怨它找不到指定的库。这里的具体示例是 ImageMagick,但我也遇到了其他一些库的类似问题。
I am calling the linker with:
我正在调用链接器:
g++ -w (..lots of .o files/include directories/etc..) \
-L/usr/lib -lmagic
ld complains:
ld 抱怨:
/usr/bin/ld: cannot find -lmagic
However, libmagic exists:
但是,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
How do I diagnose this problem further, and what could be wrong? Am I doing something completely stupid?
我如何进一步诊断这个问题,可能有什么问题?我在做一些完全愚蠢的事情吗?
采纳答案by grepsedawk
The problem is the linker is looking for libmagic.so
but you only have libmagic.so.1
问题是链接器正在寻找,libmagic.so
但您只有libmagic.so.1
A quick hack is to symlink libmagic.so.1
to libmagic.so
一个快速的技巧是符号链接libmagic.so.1
到libmagic.so
回答by Brian Gianforcaro
Unless I'm badly mistaken libmagic
or -lmagic
is not the same library as ImageMagick. You state that you want ImageMagick.
除非我大错特错libmagic
或者-lmagic
与 ImageMagick 不是同一个库。您声明您想要 ImageMagick。
ImageMagick comes with a utility to supply all appropriate options to the compiler.
ImageMagick 带有一个实用程序,可以为编译器提供所有适当的选项。
Ex:
前任:
g++ program.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs` -o "prog"
回答by Piotr Lesnicki
As just formulated by grepsedawk, the answer lies in the -l
option of g++
, calling ld
. If you look at the man page of this command, you can either do:
正如 grepsedawk 刚刚制定的那样,答案在于 的-l
选项中g++
,调用ld
. 如果您查看此命令的手册页,您可以执行以下操作:
g++ -l:libmagic.so.1 [...]
- or:
g++ -lmagic [...]
, if you have a symlink named libmagic.so in your libs path
g++ -l:libmagic.so.1 [...]
- 或者:
g++ -lmagic [...]
,如果您的库路径中有一个名为 libmagic.so 的符号链接
回答by ephemient
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 → …
).
Debian 惯例是将共享库分成它们的运行时组件 ( libmagic1: /usr/lib/libmagic.so.1 → libmagic.so.1.0.0
) 和它们的开发组件 ( 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.
因为库的 soname 是libmagic.so.1
,这是嵌入到可执行文件中的字符串,因此这是运行可执行文件时加载的文件。
However, because the library is specified as -lmagic
to the linker, it looks for libmagic.so
, which is why it is needed for development.
但是,由于库是-lmagic
针对链接器指定的,因此它会查找libmagic.so
,这就是开发需要它的原因。
See Diego E. Pettenò: Linkers and namesfor details on how this all works on Linux.
请参阅Diego E. Pettenò:链接器和名称,了解有关这一切如何在 Linux 上工作的详细信息。
In short, you should apt-get install libmagic-dev
. This will not only give you libmagic.so
but also other files necessary for compiling like /usr/include/magic.h
.
简而言之,你应该apt-get install libmagic-dev
。这不仅会为您libmagic.so
提供编译所需的其他文件,例如/usr/include/magic.h
.
回答by Mr Ed
In Ubuntu, you can install libtool
which resolves the libraries automatically.
在 Ubuntu 中,您可以安装libtool
它自动解析库。
$ 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.
这ltdl
为我解决了一个问题,该问题已经安装,libltdl.so.7
并且-lltdl
在 make 中没有找到。
回答by kirenpillay
Installing libgl1-mesa-dev from the Ubuntu repo resolved this problem for me.
从 Ubuntu 存储库安装 libgl1-mesa-dev 为我解决了这个问题。
回答by Miguel Ignacio Iglesias
As mentioned above the linker is looking for libmagic.so
, but you only have libmagic.so.1
.
如上所述,链接器正在寻找libmagic.so
,但您只有libmagic.so.1
.
To solve this problem just perform an update cache.
要解决此问题,只需执行更新缓存。
ldconfig -v
To verify you can run:
要验证您可以运行:
$ ldconfig -p | grep libmagic