我正在开发一个可以在 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/