Search code examples
clinuxgcccentosredhat

How do I resolve this error 'cannot find -lc'?


How do I compile this code in CentOS 7 ? I am reading one book and in the book they use -static while compiling so that's how I did it and I get errors I mentioned below but when I dont use -static I get no errors and it compiles successfully.

First attempt:

main()
{
        exit(0);
}

I get this error when I try to compile it.

$ gcc -static -o exit exit.c
exit.c: In function _main_:
exit.c:3:9: warning: incompatible implicit declaration of built-in function _exit_ [enabled by default]
         exit(0);
         ^
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

Second attempt:

Then I google this error and lots of articles told me to include stdlib.h library so I did that as well and I get this error: Code:

#include <stdlib.h>
main()
{
        exit(0);
}

Now when I compile it, I get following error.

$ gcc -static -o exit exit.c 
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

linux version:

$ uname -a
Linux localhost.localdomain 3.10.0-1127.13.1.el7.centos.plus.i686 #1 SMP Thu Jun 25 16:59:06 UTC 2020 i686 i686 i386 GNU/Linux

Solution

  • As @KamilCuk noted, this requires a different set of libraries for static linking, and on my CentOS 7 machine, this installs the proper libraries:

    # yum install glibc-static
    

    Then the compile should work as you expect.

    The libraries installed by this package are:

    $ rpm -q -l glibc-static
    /usr/lib64/libBrokenLocale.a
    /usr/lib64/libanl.a
    /usr/lib64/libc.a
    /usr/lib64/libc_stubs.a
    /usr/lib64/libcrypt.a
    /usr/lib64/libdl.a
    /usr/lib64/libm.a
    /usr/lib64/libnsl.a
    /usr/lib64/libpthread.a
    /usr/lib64/libresolv.a
    /usr/lib64/librt.a
    /usr/lib64/libutil.a