12
$ printf 'int main(){}' | gcc -static -x c - -o hello
$ scp hello vi-server.org:./
hello                                100%  565KB 565.2KB/s   00:00
$ ssh -t vi-server.org "./hello; uname -r"
FATAL: kernel too old
sh: line 1: 15378 Segmentation fault      ./hello
2.6.18-274....  # can't easily upgrade the kernel
Connection to vi-server.org closed.

How to build static binary that will work on on old systems? I expect static binaries to work even on 2.4.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Vi.
  • 37,014
  • 18
  • 93
  • 148

2 Answers2

14

You need to configure glibc to target the older kernel version. Per http://www.gnu.org/software/libc/manual/html_node/Configuring-and-compiling.html glibc accepts the configure option --enable-kernel=version where version is in the form 2.4.20 to target older kernel versions.

You can then link your program statically with gcc -static -nodefaultlibs [...] /path/to/my/libc.a.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
1

Thank you to the above poster ecatmur -- it does indeed work to reconfigure/rebuild glibc with the configure option --enable-kernel=version

I would add the following -- you can use gcc -static -L/path/to/local/lib (big L option to the directory) and it seems to work just as well as linking to the library file itself. When I linked in the latter fashion (to /path/to/local/lib/libc.a), it created an unecessarily large executable.

Mike J
  • 11
  • 1
  • 2
    Note: there is often an option to make "half-static" binary. It will link to libc dynamically, but to other libraries statically (you can choose which libraries to include into binary and which to link to). It can be done by manually editing the linking command. – Vi. Sep 14 '12 at 22:45