"/usr/bin/ld: cannot find -lz" but libz exists

24,711

Solution 1

Install zlib-devel or add a libz.so symlink to the lib directories.

yum list lists ALL packages, not just the installed ones. To find out whether it's installed, try this:

yum list installed | grep zlib

This is from a RHEL 6.3 reference system:

# rpm -ql zlib-devel
/usr/include/zconf.h
/usr/include/zlib.h
/usr/lib64/libz.so                  <---- the symlink I mentioned
/usr/lib64/pkgconfig/zlib.pc
/usr/share/doc/zlib-devel-1.2.3
/usr/share/doc/zlib-devel-1.2.3/README
/usr/share/doc/zlib-devel-1.2.3/algorithm.txt
/usr/share/doc/zlib-devel-1.2.3/example.c
/usr/share/doc/zlib-devel-1.2.3/minigzip.c
/usr/share/man/man3/zlib.3.gz
# rpm -ql zlib-devel.i686
/usr/include/zconf.h
/usr/include/zlib.h
/usr/lib/libz.so                    <---- the symlink I mentioned
/usr/lib/pkgconfig/zlib.pc
/usr/share/doc/zlib-devel-1.2.3
/usr/share/doc/zlib-devel-1.2.3/README
/usr/share/doc/zlib-devel-1.2.3/algorithm.txt
/usr/share/doc/zlib-devel-1.2.3/example.c
/usr/share/doc/zlib-devel-1.2.3/minigzip.c
/usr/share/man/man3/zlib.3.gz

What the symlinks point to:

# ls /usr/lib/libz.so -lhA
lrwxrwxrwx. 1 root root 23 Feb 23 20:32 /usr/lib/libz.so -> ../../lib/libz.so.1.2.3
# ls /usr/lib64/libz.so -lhA
lrwxrwxrwx. 1 root root 25 Feb 23 20:24 /usr/lib64/libz.so -> ../../lib64/libz.so.1.2.3

Solution 2

If it helps anyone - my issue was that cmake was running /usr/bin/g++ -static-libstdc++ which caused the error output: /usr/bin/ld: cannot find -lstdc++.

The fix (on RHEL/CentOS) was to run sudo yum install libstdc++-static.x86_64 since it was looking for the static lib.

Share:
24,711

Related videos on Youtube

mahmood
Author by

mahmood

Updated on September 18, 2022

Comments

  • mahmood
    mahmood almost 2 years

    While making my program on Redhat based linux (amd64), I get this error:

    /usr/bin/ld: cannot find -lz
    collect2: ld returned 1 exit status
    

    Searching internet reveals that I have t install zlib 32bit version. A quick search shows that I have that package

    [root@localhost trunk]# find / -name libz*
    /lib64/libz.so.1
    /lib64/libz.so.1.2.3
    /lib/libz.so.1
    /lib/libz.so.1.2.3
    /usr/lib64/libz.so
    /usr/lib64/purple-2/libzephyr.so
    /usr/lib64/libzip.so.1.0.0
    /usr/lib64/libzip.so.1
    

    As you can see both /lib and /lib64 contain the libz file. Next I tried to find the ldd search path and it shows that both 32 bit and 64 bit folders are included

    [root@localhost trunk]# ldconfig -v | grep -v ^$'\t'
    /usr/lib64/ctapi:
    /usr/lib64/mysql:
    /usr/lib64/tcl8.5/tclx8.4:
    /usr/lib64/xulrunner-2:
    /lib:
    /lib64:
    /usr/lib:
    /usr/lib64:
    /lib/i686: (hwcap: 0x0008000000000000)
    /lib64/tls: (hwcap: 0x8000000000000000)  
    /usr/lib64/tls: (hwcap: 0x8000000000000000)
    /usr/lib64/sse2: (hwcap: 0x0000000004000000)
    /lib/i686/nosegneg: (hwcap: 0x0028000000000000)
    

    SO I wonder why still I get that error!!!!

    UPDATE

    I installed zlib-devel (both i686 and x86_64) and here is the confirmation

     [root@localhost trunk]# yum list zlib-devel
     Installed Packages
     zlib-devel.i686     1.2.3-27.el6             @sl
     zlib-devel.x86_64   1.2.3-27.el6             @sl
    

    However still I get the same error.

    UPDATE 2

    After fixing the incorrect symbolic link, I now have

    [root@localhost trunk]# ls -l /lib/libz.so*
    lrwxrwxrwx. 1 root root    18 Feb 23 22:58 /lib/libz.so -> /usr/lib64/libz.so
    lrwxrwxrwx. 1 root root    13 Feb 18 12:06 /lib/libz.so.1 -> libz.so.1.2.3
    -rwxr-xr-x. 1 root root 75332 Dec 10  2011 /lib/libz.so.1.2.3
    

    But error still exists

    SOLUTION

    SOLUTION

    I found that I have to install static libraries. Don't know why Makefile searches for static libraries! Please refer to my comments for the name of packages.

    Thanks fuero for help

    • Admin
      Admin over 10 years
      If an answer helped you resolve your issue, you should indicate so by upvoting and accepting it. This also indicates to the system, and community, that the matter has been resolved.
  • fuero
    fuero over 11 years
    see the updated answer
  • mahmood
    mahmood over 11 years
    I think a wrong path is confusing ld. How can I find that?
  • fuero
    fuero over 11 years
    another update. don't forget to run ldconfig. Perhaps sharing how you call the compiler helps.
  • mahmood
    mahmood over 11 years
    OK. The problem with -lz will be solved by installing static version of zlib named zlib-static. Now I am getting some other -l errors like -lm and that will be solved by installing glibc-static. I don't know why the program searches for static librsries
  • hoefling
    hoefling about 3 years
    It did help me, thank you :-)