centos - 在 CentOS 8 上为 CentOS 7 编译

标签 centos glibc libstdc++

我正在开发一个可以在 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 二进制文件吗?我正在尝试使其便携。

最佳答案

To try and make the code portable, I am compiling using the following command


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

Could that be causing it?


它可以,而且确实如此。

Will I have to compile a separate CentOS 7 and CentOS 8 binary?


仅当您坚持完全静态链接时。
您可以使用动态 libc.so.6 构建您的二进制文件在 CentOS 7 上(或在 chroot 或 docker 容器或 VM 中),它也可以在 CentOS 8 上正常工作。

关于centos - 在 CentOS 8 上为 CentOS 7 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69082168/

相关文章:

linux - 单个主机上的多个 glibc 库

node.js - CentOS, Node 找不到模块

centos - 使用 PHPSecLib,调用 getSupportedVersions() 时得到空版本的原因是什么?

c - hsearch_r 是线程安全的吗?

c++ - 通过指针与数组引用进行数组索引序列扩展

C++11 Android 指针 vector push_back 不适用于 libstdc++

c++ - 静态 libstdc++.a 中的全局符号在共享 libstdc++.so 中是局部的

azure - azure 云上的私有(private)和公共(public) IP 地址

ruby-on-rails - 使用前端服务器和不同机器上的音频文件在 LAN 上运行 Ruby on rails 应用程序的流式音频?

c - -ffast-math (-ffinite-math-only) 和 glibc 2.31 的链接错误