6

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)

3
  • 1
    The CC variable is not the right place for compiler flags. You should put those in CFLAGS. Sep 30, 2014 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 :) Sep 30, 2014 at 17:47
  • If a statically linked rm is the endgoal, simply install busybox-static or sash. Sep 30, 2014 at 18:59

2 Answers 2

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
2
  • 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. Sep 30, 2014 at 17:50
  • @DavidFoerster: Good catch, thanks. I've updated my answer accordingly. Sep 30, 2014 at 18:40
3

You can retrieve a precompiled version with static-get

static-get -x coreutils
1
  • strangely this will download a 32bit x86 coreutils, so it won't work on 64bit systems =/ is there some way to download a 64bit x86-64 coreutils with static-get ?
    – hanshenrik
    Apr 13, 2021 at 22:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .