2

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
3
  • -static So did you install a static version of C standard library?
    – KamilCuk
    Jul 6, 2020 at 13:37
  • Is there a a reason for statically linking your program?
    – stark
    Jul 6, 2020 at 14:42
  • @stark This is the reason mentioned in the book "Compile this program using the static option with gcc—this prevents dynamic linking, which will preserve our exit syscall"
    – Evil Dead
    Jul 6, 2020 at 18:33

1 Answer 1

1

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.