gpt4 book ai didi

Linux静态链接死了?

转载 作者:IT老高 更新时间:2023-10-28 12:31:31 34 4
gpt4 key购买 nike

事实上,Linux 上的 -static gcc 标志现在不起作用。让我引用 GNU libc 常见问题解答:

2.22. Even statically linked programs need some shared libraries which is not acceptable for me. What can I do?

{AJ} NSS (for details just type `info libc "Name Service Switch"') won't work properly without shared libraries. NSS allows using different services (e.g. NIS, files, db, hesiod) by just changing one configuration file (/etc/nsswitch.conf) without relinking any programs. The only disadvantage is that now static libraries need to access shared libraries. This is handled transparently by the GNU C library.

A solution is to configure glibc with --enable-static-nss. In this case you can create a static binary that will use only the services dns and files (change /etc/nsswitch.conf for this). You need to link explicitly against all these services. For example:

 gcc -static test-netdb.c -o test-netdb \
-Wl,--start-group -lc -lnss_files -lnss_dns -lresolv -Wl,--end-group

The problem with this approach is that you've got to link every static program that uses NSS routines with all those libraries.
{UD} In fact, one cannot say anymore that a libc compiled with this option is using NSS. There is no switch anymore. Therefore it is highly recommended not to use --enable-static-nss since this makes the behaviour of the programs on the system inconsistent.

关于这个事实,现在是否有任何合理的方法可以在 Linux 上创建功能齐全的静态构建,或者静态链接在 Linux 上完全死了?我的意思是静态构建:

  • 行为方式与动态构建(静态 nss 与不一致的行为是邪恶的!);
  • 适用于 glibc 环境和 Linux 版本的合理变化;

最佳答案

我认为这很烦人,并且我认为将某个功能称为“无用”是傲慢的,因为它在处理某些用例时存在问题。 glibc 方法的最大问题是它对系统库(gconv 和 nss)的路径进行硬编码,因此当人们尝试在不同于构建它的 Linux 发行版上运行静态二进制文件时,它会中断。

无论如何,您可以通过将 GCONV_PATH 设置为指向适当的位置来解决 gconv 问题,这样我就可以获取在 Ubuntu 上构建的二进制文件并在 Red Hat 上运行它们。

关于Linux静态链接死了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3430400/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com