如何链接到特定的 glibc 版本?

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

How can I link to a specific glibc version?

linuxgcclinkerglibclibc

提问by falstaff

When I compile something on my Ubuntu Lucid 10.04 PC it gets linked against glibc. Lucid uses 2.11 of glibc. When I run this binary on another PC with an older glibc, the command fails saying there's no glibc 2.11...

当我在我的 Ubuntu Lucid 10.04 PC 上编译某些东西时,它会链接到 glibc。Lucid 使用 2.11 的 glibc。当我在另一台装有旧 glibc 的 PC 上运行这个二进制文件时,命令失败,说没有 glibc 2.11 ...

As far as I know glibc uses symbol versioning. Can I force gcc to link against a specific symbol version?

据我所知,glibc 使用符号版本控制。我可以强制 gcc 链接到特定的符号版本吗?

In my concrete use I try to compile a gcc cross toolchain for ARM.

在我的具体使用中,我尝试为 ARM 编译一个 gcc 交叉工具链。

采纳答案by jschmier

You are correct in that glibc uses symbol versioning. If you are curious, the symbol versioning implementation introduced in glibc 2.1 is described hereand is an extension of Sun's symbol versioning scheme described here.

您是正确的,因为 glibc 使用符号版本控制。如果你是好奇,符号版本实施的glibc 2.1介绍介绍这里是太阳的符号版本方案中描述的扩展这里

One option is to statically link your binary. This is probably the easiest option.

一种选择是静态链接您的二进制文件。这可能是最简单的选择。

You could also build your binary in a chroot build environment, or using a glibc-new=> glibc-oldcross-compiler.

您还可以在 chroot 构建环境中构建二进制文件,或者使用 glibc- new=> glibc - old交叉编译器。

According to the http://www.trevorpounds.comblog post Linking to Older Versioned Symbols (glibc), it is possible to to force any symbol to be linked against an older one so long as it is valid by using the the same .symverpseudo-op that is used for defining versioned symbols in the first place. The following example is excerpted from the blog post.

根据http://www.trevorpounds.com博客文章Linking to Older Versioned Symbols (glibc),只要使用相同的.symver伪代码有效,就可以强制任何符号链接到较旧的符号-op 首先用于定义版本化符号。以下示例摘自博客文章

The following example makes use of glibc's realpath, but makes sure it is linked against an older 2.2.5 version.

以下示例使用 glibc 的真实路径,但确保它链接到较旧的 2.2.5 版本。

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

__asm__(".symver realpath,realpath@GLIBC_2.2.5");
int main()
{
    const char* unresolved = "/lib64";
    char resolved[PATH_MAX+1];

    if(!realpath(unresolved, resolved))
        { return 1; }

    printf("%s\n", resolved);

    return 0;
}

回答by Iacchus

Link with -static. When you link with -staticthe linker embeds the library inside the executable, so the executable will be bigger, but it can be executed on a system with an older version of glibc because the program will use it's own library instead of that of the system.

-static链接。当您使用-static链接时,链接器会将库嵌入到可执行文件中,因此可执行文件会更大,但它可以在具有旧版 glibc 的系统上执行,因为该程序将使用它自己的库而不是系统的库.