【问题标题】:Compiling on CentOS 8 for CentOS 7在 CentOS 8 上为 CentOS 7 编译
【发布时间】:2021-11-03 23:51:05
【问题描述】:

我正在开发一个可以在 CentOS 8 和 CentOS 7 系统上运行的程序。在其中,我使用 gethostbyname 将 DNS 名称解析为 IP 地址。

为了尝试使代码可移植,我正在使用以下命令进行编译:

g++ -static stats-agent.cpp -o stats-agent

当它编译时,我得到以下信息:

/tmp/ccVDW5ph.o: In function `main':
stats-agent.cpp:(.text+0x45c): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

现在,当我将二进制文件移至 CentOS 7 系统并尝试运行它时,我得到以下信息:

stats-agent: dl-call-libc-early-init.c:37: _dl_call_libc_early_init: Assertion `sym != NULL' failed.
Aborted

我检查了 glibc、glib-devel 和 glibc-static。我唯一注意到的是 CentOS 8 上的 glibc 版本是 2.28,而 CentOS7 上的版本是 2.17。这可能是造成的吗?我需要编译一个单独的 CentOS 7 和 CentOS 8 二进制文件吗?我正在尝试使其可移植。

【问题讨论】:

    标签: centos glibc libstdc++


    【解决方案1】:

    为了尝试使代码可移植,我正在使用以下命令进行编译

    不要。与普遍的看法相反,完全静态的 Linux 二进制文件较少可移植——只有在运行时安装 exact 相同版本的 GLIBC 时才能保证工作,就像在运行时使用的那样构建。这就是警告告诉你的。另见this answer

    会不会是这个原因?

    它可以,而且确实如此。

    我需要编译一个单独的 CentOS 7 和 CentOS 8 二进制文件吗?

    仅当您坚持完全静态链接时。

    您可以在 CentOS 7 上使用 dynamic libc.so.6(或在 chroot、docker 容器或虚拟机中)构建您的二进制文件,它可以在 CentOS 8 上正常运行好吧。

    【讨论】:

    • 那么这是否意味着另一方面,我必须安装库?
    • @chris-p-tech No:libc.so.6 已安装在每个系统上。
    猜你喜欢
    • 2017-12-20
    • 2015-11-21
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多