如何使用旧内核为 GNU/Linux 安装构建静态二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11206243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:03:43  来源:igfitidea点击:

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

linuxgcclibc

提问by Vi.

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

如何构建可在旧系统上运行的静态二进制文件?我希望静态二进制文件即使在 2.4 上也能工作。

采纳答案by ecatmur

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

您需要配置 glibc 以针对较旧的内核版本。根据http://www.gnu.org/software/libc/manual/html_node/Configuring-and-compiling.htmlglibc 接受配置选项--enable-kernel=version,其中version的形式2.4.20是针对旧内核版本。

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

然后,您可以将您的程序静态链接到gcc -static -nodefaultlibs [...] /path/to/my/libc.a.

回答by Mike J

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

感谢上面的海报 ecatmur——它确实可以使用配置选项重新配置/重建 glibc --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.

我将添加以下内容——您可以使用 gcc -static -L/path/to/local/lib(目录的大 L 选项),它似乎与链接到库文件本身一样有效。当我以后一种方式链接(到 /path/to/local/lib/libc.a)时,它创建了一个不必要的大可执行文件。