Force gcc to compile 32 bit programs on 64 bit platform Force gcc to compile 32 bit programs on 64 bit platform linux linux

Force gcc to compile 32 bit programs on 64 bit platform


You need to make GCC use the -m32 flag.

You could try writing a simple shell script to your $PATH and call it gcc (make sure you don't overwrite the original gcc, and make sure the new script comes earlier in $PATH, and that it uses the full path to GCC.

I think the code you need is just something like /bin/gcc -m32 $* depending on your shell (the $* is there to include all arguments, although it might be something else – very important!)


You may get a 32-bit binary by applying Alan Pearce's method, but you may also get errors as follows:

fatal error: bits/predefs.h: No such file or directory

If this is the case and if you have apt-get, just install gcc-multilib

sudo apt-get install gcc-multilib 


For any code that you compile directly using gcc/g++, you will need to add -m32 option to the compilation command line, simply edit your CFLAGS, CXXFLAGS and LDFLAGS variables in your Makefile.

For any 3rd party code you might be using you must make sure when you build it to configure it for cross compilation. Run ./configure --help and see which option are available. In most cases you can provide your CFLAGS, CXXFLAGS and LDFLAGS variables to the configure script. You also might need to add --build and --host to the configure script so you end up with something like

./configure CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32 --build=x86_64-pc-linux-gnu --host=i686-pc-linux-gnu

If compilation fails this probably means that you need to install some 32 bit development packages on your 64 bit machine