本文翻译自:usr/bin/ld: cannot find -l
I'm trying to compile my program and it returns this error : 我正在尝试编译我的程序,它返回此错误:
usr/bin/ld: cannot find -l<nameOfTheLibrary>
in my makefile I use the command g++
and link to my library which is a symbolic link to my library located on an other directory. 在我的makefile文件中,我使用命令g++
并链接到我的库,这是到另一个目录中我的库的符号链接。
Is there an option to add to make it work please? 是否可以添加选项以使其正常工作?
#1楼
参考:https://stackoom.com/question/1872t/usr-bin-ld-找不到-l-nameOfTheLibrary
#2楼
When you compile your program you must supply the path to the library; 编译程序时,必须提供库的路径。 in g++ use the -L option: 在g ++中,使用-L选项:
g++ myprogram.cc -o myprogram -lmylib -L/path/foo/bar
#3楼
If your library name is say libxyz.so
and it is located on path say: 如果您的库名称为libxyz.so
且位于路径中,请说:
/home/user/myDir
then to link it to your program: 然后将其链接到您的程序:
g++ -L/home/user/myDir -lxyz myprog.cpp -o myprog
#4楼
To figure out what the linker is looking for, run it in verbose mode. 要弄清楚链接程序在寻找什么,请在详细模式下运行它。
For example, I encountered this issue while trying to compile MySQL with ZLIB support. 例如,当我尝试使用ZLIB支持编译MySQL时遇到了这个问题。 I was receiving an error like this during compilation: 我在编译期间收到这样的错误:
/usr/bin/ld: cannot find -lzlib
I did some Googl'ing and kept coming across different issues of the same kind where people would say to make sure the .so file actually exists and if it doesn't, then create a symlink to the versioned file, for example, zlib.so.1.2.8. 我做了一些Googl'ing操作,并不断遇到同类问题,人们会说要确保.so文件确实存在,如果不存在,则创建指向版本文件的符号链接,例如zlib。所以1.2.8。 But, when I checked, zlib.so DID exist. 但是,当我检查时,zlib.so DID存在。 So, I thought, surely that couldn't be the problem. 所以,我想,那肯定不是问题。
I came across another post on the Internets that suggested to run make with LD_DEBUG=all: 我在互联网上遇到了另一条建议使用LD_DEBUG = all运行make的帖子:
LD_DEBUG=all make
Although I got a TON of debugging output, it wasn't actually helpful. 尽管我得到了大量调试信息,但实际上并没有帮助。 It added more confusion than anything else. 它比其他任何事情都增加了混乱。 So, I was about to give up. 所以,我正要放弃。
Then, I had an epiphany. 然后,我顿悟了。 I thought to actually check the help text for the ld command: 我认为实际上要检查ld命令的帮助文本:
ld --help
From that, I figured out how to run ld in verbose mode (imagine that): 由此,我想出了如何在详细模式下运行ld(想象一下):
ld -lzlib --verbose
This is the output I got: 这是我得到的输出:
==================================================
attempt to open /usr/x86_64-linux-gnu/lib64/libzlib.so failed
attempt to open /usr/x86_64-linux-gnu/lib64/libzlib.a failed
attempt to open /usr/local/lib64/libzlib.so failed
attempt to open /usr/local/lib64/libzlib.a failed
attempt to open /lib64/libzlib.so failed
attempt to open /lib64/libzlib.a failed
attempt to open /usr/lib64/libzlib.so failed
attempt to open /usr/lib64/libzlib.a failed
attempt to open /usr/x86_64-linux-gnu/lib/libzlib.so failed
attempt to open /usr/x86_64-linux-gnu/lib/libzlib.a failed
attempt to open /usr/local/lib/libzlib.so failed
attempt to open /usr/local/lib/libzlib.a failed
attempt to open /lib/libzlib.so failed
attempt to open /lib/libzlib.a failed
attempt to open /usr/lib/libzlib.so failed
attempt to open /usr/lib/libzlib.a failed
/usr/bin/ld.bfd.real: cannot find -lzlib
Ding, ding, ding... 叮,叮...
So, to finally fix it so I could compile MySQL with my own version of ZLIB (rather than the bundled version): 因此,最后修复它,以便可以使用自己的ZLIB版本(而不是捆绑版本)来编译MySQL:
sudo ln -s /usr/lib/libz.so.1.2.8 /usr/lib/libzlib.so
Voila! 瞧!
#5楼
During compilation with g++
via make
define LIBRARY_PATH
if it may not be appropriate to change the Makefile with the -L
option. 在使用g++
进行编译的过程中,如果可能不适合使用-L
选项更改Makefile,则通过make
定义LIBRARY_PATH
。 I had put my extra library in /opt/lib
so I did: 我将额外的库放在/opt/lib
所以我这样做了:
$ export LIBRARY_PATH=/opt/lib/
and then ran make
for successful compilation and linking. 然后运行make
进行成功的编译和链接。
To run the program with a shared library define: 要使用共享库运行程序,请定义:
$ export LD_LIBRARY_PATH=/opt/lib/
before executing the program. 在执行程序之前。
#6楼
Compile Time 编译时间
When g++ says cannot find -l<nameOfTheLibrary>
, it means that g++ looked for the file lib{nameOfTheLibrary}.so
, but it couldn't find it in the shared library search path, which by default points to /usr/lib
and /usr/local/lib
and somewhere else maybe. 当g ++说cannot find -l<nameOfTheLibrary>
,表示g ++在lib{nameOfTheLibrary}.so
文件中寻找,但在共享库搜索路径中找不到它,默认情况下它指向/usr/lib
和/usr/local/lib
和其他地方。
To resolve this problem, you should either provide the library file ( lib{nameOfTheLibrary}.so
) in those search paths or use -L
command option. 要解决此问题,您应该在这些搜索路径中提供库文件( lib{nameOfTheLibrary}.so
),或使用-L
命令选项。 -L{path}
tells the g++ (actually ld
) to find library files in path {path}
in addition to default paths. -L{path}
告诉g ++(实际上是ld
)在默认路径之外查找路径{path}
中的库文件。
Example: Assuming you have a library at /home/taylor/libswift.so
, and you want to link your app to this library. 示例:假设您在/home/taylor/libswift.so
有一个库,并且想要将应用程序链接到该库。 In this case you should supply the g++ with the following options: 在这种情况下,您应该为g ++提供以下选项:
g++ main.cpp -o main -L/home/taylor -lswift
Note 1 :
-l
option gets the library name withoutlib
and.so
at its beginning and end. 注1 :-l
选项在开始和结束时获取不带lib
和.so
的库名。Note 2 : In some cases, the library file name is followed by its version, for instance
libswift.so.1.2
. 注2 :在某些情况下,库文件名后跟版本,例如libswift.so.1.2
。 In these cases, g++ also cannot find the library file. 在这些情况下,g ++也找不到该库文件。 A simple workaround to fix this is creating a symbolic link tolibswift.so.1.2
calledlibswift.so
. 解决此问题的简单方法是创建指向libswift.so.1.2
的符号链接,称为libswift.so
。
Runtime 运行
When you link your app to a shared library, it's required that library stays available whenever you run the app. 当您将应用程序链接到共享库时,需要在运行该应用程序时该库保持可用。 In runtime your app (actually dynamic linker) looks for its libraries in LD_LIBRARY_PATH
. 在运行时,您的应用程序(实际上是动态链接器)在LD_LIBRARY_PATH
查找其库。 It's an environment variable which stores a list of paths. 这是一个环境变量,用于存储路径列表。
Example: In case of our libswift.so
example, dynamic linker cannot find libswift.so
in LD_LIBRARY_PATH
(which points to default search paths). 示例:在我们的libswift.so
示例中,动态链接程序无法在LD_LIBRARY_PATH
(指向默认搜索路径)中找到libswift.so
。 To fix the problem you should append that variable with the path libswift.so
is in. 要解决此问题,您应该将该变量附加libswift.so
所在的路径。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/taylor