5

I'm trying to make a static binary of rm, so I tried compiling coreutils with

configure; make SHARED=0 CC='gcc -static'; make install

but I keep running into errors.

First make complains about c99 or gnu99, I solved that by adding -std=gnu99 to the CC value:

make SHARED=0 CC='gcc -static -std=gnu99'

After that it says:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbeginT.o: relocation R_X86_64_32 against `__TMC_END__' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.8/crtbeginT.o: error adding symbols: Bad value

I tried adding -fPIC like I added the -std option but to no avail.

Is it even possible to make static binaries of coreutils? (only interested in rm) If so, how? (I have 0 experience with building linux binaries)

PS: I downloaded the coreutils source from the ubuntu site, trusty.(coreutils-8.21)

  • 1
    The CC variable is not the right place for compiler flags. You should put those in CFLAGS. – David Foerster Sep 30 '14 at 16:20
  • so while Sylvain's answer works, it could be improved by using CFLAGS instead of CC? Again, I know nothing about compiling C, I'm just a stupid Java programmer :) – Nicolas Mommaerts Sep 30 '14 at 17:47
  • If a statically linked rm is the endgoal, simply install busybox-static or sash. – Gilles Sep 30 '14 at 18:59
7

Per this comment on bug #640734, there is a workaround that you may try:

$ cd /usr/lib/gcc/x86_64-linux-gnu/4.8
$ sudo cp crtbeginT.o crtbeginT.orig.o
$ sudo cp crtbeginS.o crtbeginT.o

Also, add -fPIC to the command above:

make SHARED=0 CFLAGS='-static -std=gnu99 -static-libgcc -static-libstdc++ -fPIC'

Once you're done with the compilation just restore the original file like this:

$ cd /usr/lib/gcc/x86_64-linux-gnu/4.8
$ sudo cp crtbeginT.orig.o crtbeginT.o
$ sudo rm crtbeginT.orig.o
  • As mentioned in my other comment, this solution can be improved, if you replace CC='gcc -static …' by CFLAGS='-static …'. CC is meant to contain the C compiler to use, CFLAGS is dedicated to C compiler flags. – David Foerster Sep 30 '14 at 17:50
  • @DavidFoerster: Good catch, thanks. I've updated my answer accordingly. – Sylvain Pineau Sep 30 '14 at 18:40
3

You can retrieve a precompiled version with static-get

static-get -x coreutils

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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