C语言 创建使用 getaddrinfo 的静态链接二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2725255/
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-09-02 05:18:06  来源:igfitidea点击:

Create statically-linked binary that uses getaddrinfo?

cgccposixc99static-linking

提问by Neeladri Vishweswaran

I have included the header netdb.h, where getaddrinfois included, but gcc issues this warning:

我已经包含了 header netdb.h,其中getaddrinfo包含,但 gcc 发出此警告:

warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

gcc -m32 -static -s -O2 -std=c99 -D_POSIX_C_SOURCE=200112L myprogram.c

How can I statically compile whatever file is missing ?

如何静态编译丢失的任何文件?

Possible solutions:

可能的解决方案:

  1. It could be that the glibc installation is missing the corresponding object file necessary for static compilation. If that is the case, create the corresponding object file and link it at compilation.

  2. Try EGLIBC instead of glibc.

  3. I succesfully compiled my program with dietlibc which compiled without any errors plus the resulting binary was much smaller than what glibc makes.

  1. 可能是 glibc 安装缺少静态编译所需的相应目标文件。如果是这种情况,请创建相应的目标文件并在编译时链接它。

  2. 尝试 EGLIBC 而不是 glibc。

  3. 我成功地用dietlibc 编译了我的程序,它编译没有任何错误,而且生成的二进制文件比glibc 生成的要小得多。

采纳答案by bdonlan

glibc uses libnss to support a number of different providers for address resolution services. Unfortunately, you cannot statically link libnss, as exactly what providers it loads depends on the local system's configuration.

glibc 使用 libnss 来支持许多不同的地址解析服务提供者。不幸的是,您不能静态链接 libnss,因为它加载的提供程序取决于本地系统的配置。

回答by zhanxw

I found a solution: you can use musl library to replace glibc. To use musl, you can either install it and build your software using musl-gcc, or you can use a Linux distribution that uses musl, e.g. Alpine Linux.

我找到了一个解决方案:您可以使用 musl 库来替换 glibc。要使用 musl,您可以使用 musl-gcc 安装它并构建您的软件,或者您可以使用使用 musl 的 Linux 发行版,例如 Alpine Linux。

In my case, to save time, I chose Alpine Linux to build my program (https://github.com/zhanxw/rvtests), as I don't want to build multiple compilers (gcc, g++ and gfortran).

就我而言,为了节省时间,我选择了 Alpine Linux 来构建我的程序(https://github.com/zhanxw/rvtests),因为我不想构建多个编译器(gcc、g++ 和 gfortran)。

回答by pixelbeat

I think certain features are dependent on the dynamic loader to work things out at run time. static linking is no longer practical unfortunately http://people.redhat.com/drepper/no_static_linking.html

我认为某些功能依赖于动态加载器在运行时解决问题。不幸的是,静态链接不再实用http://people.redhat.com/drepper/no_static_linking.html