Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

gcc

libc

$ 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.

like image 707
Vi. Avatar asked Jun 26 '12 11:06

Vi.


1 Answers

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.

like image 119
ecatmur Avatar answered Sep 28 '22 15:09

ecatmur