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

LinuxGcc64 BitCmake32 Bit

Linux Problem Overview


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?

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.

Linux Solutions


Solution 1 - Linux

export CFLAGS=-m32

Solution 2 - Linux

$ 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.

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)

Solution 3 - Linux

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

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

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

Solution 4 - Linux

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'.

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

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.

Solution 5 - Linux

For C++, you could do:

export CXXFLAGS=-m32

This works with cmake.

Solution 6 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondalaView Question on Stackoverflow
Solution 1 - LinuxcafView Answer on Stackoverflow
Solution 2 - LinuxandriView Answer on Stackoverflow
Solution 3 - LinuxNathan MonteleoneView Answer on Stackoverflow
Solution 4 - LinuxSam WatkinsView Answer on Stackoverflow
Solution 5 - LinuxeuccasView Answer on Stackoverflow
Solution 6 - LinuxDirk EddelbuettelView Answer on Stackoverflow