创建使用 getaddrinfo 的静态链接二进制文件?
我已经包含了标头 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 staticallycompile 无论什么文件丢失?
可能的解决方案:
可能是glibc安装缺少静态编译所需的相应目标文件。如果是这种情况,请创建相应的目标文件并在编译时链接它。
尝试使用 EGLIBC 而不是 glibc。
我成功地使用 Dietlibc 编译了我的程序,它编译时没有任何错误,而且生成的二进制文件比 glibc 生成的小得多。
我成功地使用 Dietlibc 编译了我
I have included the header netdb.h
, where getaddrinfo
is included, but gcc issues this warning:
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:
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.
Try EGLIBC instead of glibc.
I succesfully compiled my program with dietlibc which compiled without any errors plus the resulting binary was much smaller than what glibc makes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
glibc 使用 libnss 支持许多不同的地址解析服务提供者。不幸的是,您无法静态链接 libnss,因为它加载的提供程序具体取决于本地系统的配置。
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.
您可以使用 musl 库来替换 glibc。
要使用 musl,您可以安装它并使用 musl-gcc 构建您的软件,
或者您可以使用使用 musl 的 Linux 发行版,例如 Alpine Linux。
就我而言,为了节省时间,我选择 Alpine Linux 来构建我的程序 (https://github.com/zhanxw /rvtests),因为我不想构建多个编译器(gcc、g++ 和 gfortran)。
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.
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).
我认为某些功能依赖于动态加载器在运行时解决问题。不幸的是,静态链接不再实用 http://people.redhat.com/drepper/no_static_linking.html
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
另一种解决方案是找到丢失的文件并使用指向丢失文件的目录的符号链接。例如。
在我的特定场景中,我缺少
/usr/lib/x86_64-linux-gnu/libm-2.31.a
但我们发现这些文件实际上位于/usr/x86_64-linux -gnu/lib/libm-2.31.a
改为(lib
已移动)。因此,为丢失的文件创建符号链接就可以了。Another solution is to find the missing files and use a symlink to the directory that is missing the files. For instance.
In my particular scenario, I was missing
/usr/lib/x86_64-linux-gnu/libm-2.31.a
but we found that the files were actually located at/usr/x86_64-linux-gnu/lib/libm-2.31.a
instead (thelib
is shifted). So creating a symlink for that missing files did the trick.