How to build a static binary for GNU/Linux installations with old kernel?

Vi. picture Vi. · Jun 26, 2012 · Viewed 21.1k times · Source
$ 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.

Answer

ecatmur picture ecatmur · Jun 26, 2012

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.