Rectangle 27 14

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.

To resolve this problem, you should either provide the library file (lib{nameOfTheLibrary}.so) in those search paths or use -L command option. -L{path} tells the G++ (actually ld) to find library files in path {path} in addition to default paths.

Example: Assuming you have a library at /home/taylor/libswift.so, and you want to link your app to this library. In this case you should supply the G++with the following options:

g++ main.cpp -o main -L/home/taylor -lswift

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. 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). To fix the problem you should append that variable with the path libswift.so is in.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/taylor

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 14

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.

To resolve this problem, you should either provide the library file (lib{nameOfTheLibrary}.so) in those search paths or use -L command option. -L{path} tells the G++ (actually ld) to find library files in path {path} in addition to default paths.

Example: Assuming you have a library at /home/taylor/libswift.so, and you want to link your app to this library. In this case you should supply the G++with the following options:

g++ main.cpp -o main -L/home/taylor -lswift

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. 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). To fix the problem you should append that variable with the path libswift.so is in.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/taylor

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 278

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. 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. But, when I checked, zlib.so DID exist. 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

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 --help

From that, I figured out how to run ld in verbose mode (imagine that):

ld -lzlib --verbose
==================================================
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

So, to finally fix it so I could compile MySQL with my own version of ZLIB (rather than the bundled version):

sudo ln -s /usr/lib/libz.so.1.2.8 /usr/lib/libzlib.so

Thanks, this was helpful. For others using gcc to compile and link their program (rather than using ld directly), you can add -Xlinker --verbose to gcc's command-line arguments to have it pass this option to ld.

This also helped me. The Makefile I had was only expecting static libraries so it used -Wl,-Bstatic. This limits the search to .a files only. The verbose option showed this clearly. Once I removed -Wl,-Bstatic shared libraries were also searched.

I'm on FreeBSD 10. The new LLVM Clang cc takes an argument of the form -Wl,--verbose and passes --verbose to the linker.

Now that's what I call a perfect answer! Thanks a lot. It saved a lot of time. Just to add to help someone like me. It can be used to debug path related issues as well. Make sure you check the path with -L<path to directory> with command, ld -L<path> -l<library name> --verbose

-Wl,--verbose

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 278

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. 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. But, when I checked, zlib.so DID exist. 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

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 --help

From that, I figured out how to run ld in verbose mode (imagine that):

ld -lzlib --verbose
==================================================
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

So, to finally fix it so I could compile MySQL with my own version of ZLIB (rather than the bundled version):

sudo ln -s /usr/lib/libz.so.1.2.8 /usr/lib/libzlib.so

Thanks, this was helpful. For others using gcc to compile and link their program (rather than using ld directly), you can add -Xlinker --verbose to gcc's command-line arguments to have it pass this option to ld.

This also helped me. The Makefile I had was only expecting static libraries so it used -Wl,-Bstatic. This limits the search to .a files only. The verbose option showed this clearly. Once I removed -Wl,-Bstatic shared libraries were also searched.

I'm on FreeBSD 10. The new LLVM Clang cc takes an argument of the form -Wl,--verbose and passes --verbose to the linker.

Now that's what I call a perfect answer! Thanks a lot. It saved a lot of time. Just to add to help someone like me. It can be used to debug path related issues as well. Make sure you check the path with -L<path to directory> with command, ld -L<path> -l<library name> --verbose

-Wl,--verbose

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 99

If your library name is say libxyz.so and it is located on path say:

/home/user/myDir
g++ -L/home/user/myDir -lxyz myprog.cpp -o myprog

my library is not a dynamic one (.so) but a static one (.a). Does the problem come from that?

@ZoOo that shouldn't normally matter, the linker can work with either one

another way to link your library is that you can directly specify the name of the library with complete path, like g++ .. /path/mylib.a

Yes but it still doesn't work. My library is a symbolic link, i think the problem come from that because when i use the library in the other directory it works !

Is your symbolic link correctly pointing to the library at actual location??. can you post the output of "ll" on the symbolic link.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 99

If your library name is say libxyz.so and it is located on path say:

/home/user/myDir
g++ -L/home/user/myDir -lxyz myprog.cpp -o myprog

my library is not a dynamic one (.so) but a static one (.a). Does the problem come from that?

@ZoOo that shouldn't normally matter, the linker can work with either one

another way to link your library is that you can directly specify the name of the library with complete path, like g++ .. /path/mylib.a

Yes but it still doesn't work. My library is a symbolic link, i think the problem come from that because when i use the library in the other directory it works !

Is your symbolic link correctly pointing to the library at actual location??. can you post the output of "ll" on the symbolic link.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 17

During compilation with g++ via make define LIBRARY_PATH if it may not be appropriate to change the Makefile with the -Loption. I had put my extra library in /opt/lib so I did:

$ export LIBRARY_PATH=/opt/lib/

and then ran make for successful compilation and linking.

To run the program with a shared library define:

$ export LD_LIBRARY_PATH=/opt/lib/

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 17

During compilation with g++ via make define LIBRARY_PATH if it may not be appropriate to change the Makefile with the -Loption. I had put my extra library in /opt/lib so I did:

$ export LIBRARY_PATH=/opt/lib/

and then ran make for successful compilation and linking.

To run the program with a shared library define:

$ export LD_LIBRARY_PATH=/opt/lib/

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 6

There does not seem to be any answer which addresses the very common beginner problem of failing to install the required library in the first place.

On Debianish platforms, if libfoo is missing, you can frequently install it with something like

apt-get install libfoo-dev

The -dev version of the package is required for development work, even trivial development work such as compiling source code to link to the library.

The package name will sometimes require some decorations (libfoo0-dev? foo-dev without the lib prefix? etc), or you can simply use your distro's package search to find out precisely which packages provide a particular file.

(If there is more than one, you will need to find out what their differences are. Picking the coolest or the most popular is a common shortcut, but not an acceptable procedure for any serious development work.)

For other architectures (most notably RPM) similar procedures apply, though the details will be different.

This just helped me with an issue I was having with a fresh server and Perl. apt-get install libperl-dev sorted it for me. Thanks :)

This is probably the most common solution and helped me compile cacti-spine on CentOS 7. A simple yum install openssl-devel solved it.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 6

There does not seem to be any answer which addresses the very common beginner problem of failing to install the required library in the first place.

On Debianish platforms, if libfoo is missing, you can frequently install it with something like

apt-get install libfoo-dev

The -dev version of the package is required for development work, even trivial development work such as compiling source code to link to the library.

The package name will sometimes require some decorations (libfoo0-dev? foo-dev without the lib prefix? etc), or you can simply use your distro's package search to find out precisely which packages provide a particular file.

(If there is more than one, you will need to find out what their differences are. Picking the coolest or the most popular is a common shortcut, but not an acceptable procedure for any serious development work.)

For other architectures (most notably RPM) similar procedures apply, though the details will be different.

This just helped me with an issue I was having with a fresh server and Perl. apt-get install libperl-dev sorted it for me. Thanks :)

This is probably the most common solution and helped me compile cacti-spine on CentOS 7. A simple yum install openssl-devel solved it.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 20

Add -L/opt/lib to your compiler parameters, this makes the compiler and linker search that path for libcalc.so in that folder.

Oh yeah I tried that too, still I got the same error :( see the following alwin@alwin-laptop:~/Desktop/CPPTest$ g++ -Wall -I/home/alwin/Development/Calculator/ -L/opt/lib main.cpp -libcalc.so -o calculator /usr/bin/ld: cannot find -libcalc.so collect2: ld returned 1 exit status

@Jay it's -lcalc, not -libcalc.so

c++ - /usr/bin/ld: cannot find - Stack Overflow

c++ linker shared
Rectangle 27 2

This error may also be brought about if the symbolic link is to a dynamic library, .so, but for legacy reasons -static appears among the link flags. If so, try removing it.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 2

This error may also be brought about if the symbolic link is to a dynamic library, .so, but for legacy reasons -static appears among the link flags. If so, try removing it.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 1

When you compile your program you must supply the path to the library; in g++ use the -L option:

g++ myprogram.cc -o myprogram -lmylib -L/path/foo/bar

thanks, i already do that but it doesn't work

Which property do we have to change in ccmake so that the Makefile is created with the linked flag? I want to link my -lARToolkitPlus flag to a path.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 1

When you compile your program you must supply the path to the library; in g++ use the -L option:

g++ myprogram.cc -o myprogram -lmylib -L/path/foo/bar

thanks, i already do that but it doesn't work

Which property do we have to change in ccmake so that the Makefile is created with the linked flag? I want to link my -lARToolkitPlus flag to a path.

c++ - usr/bin/ld: cannot find -l - Stack Overflow

c++ linux g++
Rectangle 27 139

I had the exact same error, and like you, installing zlib1g-dev did not fix it. Installing lib32z1-dev got me past it. I have a 64 bit system and it seems like it wanted the 32 bit library.

It worked here with libz-dev (virtual package for zlib1g-dev) on Kubuntu 12.04 x64.

helped me on a totally different problem. Thanks :-)

linux - "/usr/bin/ld: cannot find -lz" - Stack Overflow

linux gcc linker
Rectangle 27 139

I had the exact same error, and like you, installing zlib1g-dev did not fix it. Installing lib32z1-dev got me past it. I have a 64 bit system and it seems like it wanted the 32 bit library.

It worked here with libz-dev (virtual package for zlib1g-dev) on Kubuntu 12.04 x64.

helped me on a totally different problem. Thanks :-)

linux - "/usr/bin/ld: cannot find -lz" - Stack Overflow

linux gcc linker
Rectangle 27 2

-L/opt/lib
ld

Like I already mentioned in the previous comment, I did add -L/opt/lib is there anything else that I am missing :(

c++ - /usr/bin/ld: cannot find - Stack Overflow

c++ linker shared
Rectangle 27 2

g++ -Wall -I/home/alwin/Development/Calculator/ -L/opt/lib main.cpp -lcalc -o calculator

not -libcalc.so

I have a similar problem with auto-generated makes.

You can create a soft link from your compile directory to the library directory. Then the library becomes "local".

cd /compile/directory

ln -s  /path/to/libcalc.so libcalc.so

c++ - /usr/bin/ld: cannot find - Stack Overflow

c++ linker shared
Rectangle 27 62

sudo apt-get install zlib1g-dev

I don't need all the x86 libs ;)

can confirm this worked for install lxml via pip on ubuntu 14.04

same for me as for mwjackson, although I got zlib1g-dev via the metapackage libz-dev, as was suggested in another answer.

can confirm zlib1g-dev worked well on Debian 8 Jessie 64 bit

zlib-static

linux - "/usr/bin/ld: cannot find -lz" - Stack Overflow

linux gcc linker