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

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


$ gcc test.c -o testc$ file testctestc: 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 testctestc: 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)


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.