如何使用 gcc/cmake 在 64 位 linux 机器上编译 32 位二进制文​​件

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

How to compile a 32-bit binary on a 64-bit linux machine with gcc/cmake

linuxgcc64-bitcmake32-bit

提问by dala

Is it possible to compile a project in 32-bitwith cmakeand gccon a 64-bitsystem? It probably is, but how do I do it?

是否有可能编译项目在32位cmakegcc一对64位系统?它可能是,但我该怎么做?

When I tried it the "ignorant" way, without setting any parameters/flags/etc, just setting LD_LIBRARY_PATHto find the linked libraries in ~/tools/libit seems to ignore it and only look in subdirectories named lib64.

当我以“无知”的方式尝试它时,没有设置任何参数/标志/等,只是设置LD_LIBRARY_PATH在其中查找链接库~/tools/lib似乎忽略了它,只查看名为lib64 的子目录。

采纳答案by caf

export CFLAGS=-m32

回答by Dirk Eddelbuettel

One way is to setup a chroot environment. Debian has a number of tools for that, for example debootstrap

一种方法是设置 chroot 环境。Debian 有许多工具,例如debootstrap

回答by andri

$ gcc test.c -o testc
$ file testc
testc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ldd testc 
    linux-vdso.so.1 =>  (0x00007fff227ff000)
    libc.so.6 => /lib64/libc.so.6 (0x000000391f000000)
    /lib64/ld-linux-x86-64.so.2 (0x000000391ec00000)
$ gcc -m32 test.c -o testc
$ file testc
testc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ldd testc
    linux-gate.so.1 =>  (0x009aa000)
    libc.so.6 => /lib/libc.so.6 (0x00780000)
    /lib/ld-linux.so.2 (0x0075b000)

In short: use the -m32flag to compile a 32-bit binary.

简而言之:使用-m32标志来编译 32 位二进制文​​件。

Also, make sure that you have the 32-bit versions of all required libraries installed (in my case all I needed on Fedora was glibc-devel.i386)

另外,请确保您安装了所有必需库的 32 位版本(在我的情况下,我在 Fedora 上只需要 glibc-devel.i386)

回答by Nathan Monteleone

In later versions of CMake, one way to do it on each target is:

在更高版本的 CMake 中,在每个目标上执行此操作的一种方法是:

set_target_properties(MyTarget PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

set_target_properties(MyTarget PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

I don't know of a way to do it globally.

我不知道有什么方法可以在全球范围内做到这一点。

回答by Sam Watkins

For any complex application, I suggest to use an lxc container. lxc containers are 'something in the middle between a chroot on steroids and a full fledged virtual machine'.

对于任何复杂的应用程序,我建议使用lxc 容器。lxc 容器是“介于类固醇的 chroot 和成熟的虚拟机之间的东西”。

For example, here's a way to build 32-bit wine using lxc on an Ubuntu Trusty system:

例如,这里有一种在 Ubuntu Trusty 系统上使用 lxc 构建 32 位 wine 的方法:

sudo apt-get install lxc lxc-templates
sudo lxc-create -t ubuntu -n my32bitbox -- --bindhome $LOGNAME -a i386 --release trusty
sudo lxc-start -n my32bitbox
# login as yourself
sudo sh -c "sed s/deb/deb-src/ /etc/apt/sources.list >> /etc/apt/sources.list"
sudo apt-get install devscripts
sudo apt-get build-dep wine1.7
apt-get source wine1.7
cd wine1.7-*
debuild -eDEB_BUILD_OPTIONS="parallel=8" -i -us -uc -b
shutdown -h now   # to exit the container

Here is the wiki page about how to build 32-bit wine on a 64-bit host using lxc.

这是关于如何使用 lxc 在 64 位主机上构建 32 位 wine的 wiki 页面。

回答by euccas

For C++, you could do:

对于 C++,你可以这样做:

export CXXFLAGS=-m32

This works with cmake.

这适用于 cmake。