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

时间:2022-09-01 10:22:44

Is it possible to compile a project in 32-bit with cmake and gcc on a 64-bit system? It probably is, but how do I do it?

是否可以在64位系统中使用cmake和gcc来编译一个32位的项目?可能是,但我该怎么做呢?

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.

当我尝试“不知道”的方法时,没有设置任何参数/标记/etc,只是设置LD_LIBRARY_PATH来查找~/tools/lib中的链接库,它似乎忽略了它,只在名为lib64的子目录中查找。

6 个解决方案

#1


116  

export CFLAGS=-m32

#2


70  

$ 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)。

#3


15  

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属性COMPILE_FLAGS“-m32”LINK_FLAGS“-m32”)

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

我不知道如何在全球范围内做到这一点。

#4


5  

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容器是“介于类固醇和成熟虚拟机之间的东西”。

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

例如,这里有一种使用lxc在Ubuntu信任系统上构建32位葡萄酒的方法:

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.

这里是wiki页面,介绍如何使用lxc在64位主机上构建32位的葡萄酒。

#5


5  

For C++, you could do:

对于c++,您可以这样做:

export CXXFLAGS=-m32

This works with cmake.

这与cmake的作品。

#6


4  

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

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

#1


116  

export CFLAGS=-m32

#2


70  

$ 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)。

#3


15  

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属性COMPILE_FLAGS“-m32”LINK_FLAGS“-m32”)

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

我不知道如何在全球范围内做到这一点。

#4


5  

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容器是“介于类固醇和成熟虚拟机之间的东西”。

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

例如,这里有一种使用lxc在Ubuntu信任系统上构建32位葡萄酒的方法:

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.

这里是wiki页面,介绍如何使用lxc在64位主机上构建32位的葡萄酒。

#5


5  

For C++, you could do:

对于c++,您可以这样做:

export CXXFLAGS=-m32

This works with cmake.

这与cmake的作品。

#6


4  

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

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