gpt4 book ai didi

ubuntu - 如何在 64 位 Ubuntu 上编译 32 位应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:53 28 4
gpt4 key购买 nike

我正在尝试使用 gcc 4.8 在 Ubuntu Server 12.04 LTS 64 位上编译一个 32 位 C 应用程序。我收到有关不兼容库和 skipping -lgcc 的链接器错误消息。我需要做什么才能编译和链接 32 位应用程序?

最佳答案

已知这可以在 Ubuntu 16.04 到 22.04 上运行:

sudo apt install gcc-multilib g++-multilib

然后是一个最小的 hello world:

主.c

#include <stdio.h>

int main(void) {
puts("hello world");
return 0;
}

在没有警告的情况下编译:

gcc -m32 -ggdb3 -O0 -pedantic-errors -std=c89 \
-Wall -Wextra -pedantic -o main.out main.c

./main.out

输出:

hello world

和:

file main.out

说:

main.out: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=87c87a83878ce7e7d23b6236e4286bf1daf59033, not stripped

和:

qemu-i386 main.out

还给出:

hello world

但在 x86_64 可执行文件上失败:

./main.out: Invalid ELF image for this architecture

此外,我还有:

所以我认为它有效:-)

另请参阅:Cannot find crtn.o, linking 32 bit code on 64 bit system

遗憾的是这个包与像gcc-arm-linux-gnueabihf这样的交叉编译器冲突https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1300211

问题的运行版本:

我们能够直接在 64 位 Ubuntu 上运行 32 位程序,因为 Ubuntu 内核配置有:

CONFIG_IA32_EMULATION=y

根据:

grep CONFIG_IA32_EMULATION "/boot/config-$(uname -r)"

谁在 kernel source tree 上提供帮助阅读:

Include code to run legacy 32-bit programs under a
64-bit kernel. You should likely turn this on, unless you're
100% sure that you don't have any 32-bit programs left.

这又是可能的,因为 x86 64 位 CPU 具有运行 Linux 内核使用的 32 位程序的模式。

TODO:gcc-multilib 的编译选项与 gcc 有哪些不同?

关于ubuntu - 如何在 64 位 Ubuntu 上编译 32 位应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22355436/

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