Force gcc to compile 32 bit programs on 64 bit platform

LinuxGcc32 BitX86 64

Linux Problem Overview


I've got a proprietary program that I'm trying to use on a 64 bit system.

When I launch the setup it works ok, but after it tries to update itself and compile some modules and it fails to load them.

I'm suspecting it's because it's using gcc and gcc tries to compile them for a 64 bit system and therefore this program cannot use these modules.

Is there any way (some environmental variables or something like that) to force gcc to do everything for a 32 bit platform. Would a 32 bit chroot work?

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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 

Solution 3 - Linux

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

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
QuestionJure1873View Question on Stackoverflow
Solution 1 - LinuxAlan PearceView Answer on Stackoverflow
Solution 2 - LinuxGeoffrey R.View Answer on Stackoverflow
Solution 3 - Linuxe271p314View Answer on Stackoverflow