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