简体   繁体   English

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

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

Is it possible to compile a project in 32-bit with cmake and gcc on a 64-bit system? 是否可以在64位系统上使用cmakegcc编译32位项目? It probably is, but how do I do it? 它可能是,但我该怎么做?

When I tried it the "ignorant" way, without setting any parameters/flags/etc, just setting LD_LIBRARY_PATH to find the linked libraries in ~/tools/lib it seems to ignore it and only look in subdirectories named lib64 . 当我尝试“无知”的方式,没有设置任何参数/标志/等,只是设置LD_LIBRARY_PATH来找到~/tools/lib的链接库它似乎忽略它,只查看名为lib64的子目录。

export CFLAGS=-m32
$ 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 -m32 flag 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)

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")

I don't know of a way to do it globally. 我不知道在全球范围内做到这一点的方法。

For C++, you could do: 对于C ++,你可以这样做:

export CXXFLAGS=-m32

This works with cmake. 这适用于cmake。

One way is to setup a chroot environment. 一种方法是设置chroot环境。 Debian has a number of tools for that, for example debootstrap Debian有很多工具,比如debootstrap

For any complex application, I suggest to use an lxc container . 对于任何复杂的应用程序,我建议使用lxc容器 lxc containers are 'something in the middle between a chroot on steroids and a full fledged virtual machine'. 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页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM