Hacking, Coding and Gaming | @[email protected]

If you're running on a 64bit cpu but need to compile some code to 32bit, you can usually get away with doing:

gcc <filename.c> -o <filename> -m32

or in the case of "configure" and "make" you can probably go with:

./configure --build=i686-pc-linux-gnu "CFLAGS=-m32"

Unless of course you're greeted with this error:

configure: error: C compiler cannot create executables

The solution to which is (on Debian-based distros at least) hopefully just:

apt-get install lib32z1-dev

OR... we can use Docker to make life a whole lot easier (and open compilation up to even more cpu architectures), using Dockcross. Assuming you have docker installed and running, the installation instructions are fairly straightforward... you run the image(s) of the architectures you want to compile for, which results in a helper script that you use for compilation. It's probably best to output the helper script to it's own directory and using the name of the architecture it's for (and making it executable).

Inside the directory of code you want to compile (which gets mounted in to docker as "/build") you simply run something like:

~/dockcross/linux-x86 ./configure
~/dockcross/linux-x86 make

(assuming the code base makes use of configure and make, that is)

For compiling single files or more custom build commands, you can execute the command through bash and make use of the environment variables provided in the conatiner:

~/dockcross/linux-x86 make bash -c '$CC <filename.c> -o <filename>'

(where $CC would be "gcc")

So far I've had great luck using it, and even been able to compile things statically (passing in the relevant flags of course).

EDIT:  @wicusross reminded me of crosstool-ng which provides loads of options for cross compilation - personally I always seemed to end up with failed builds so prefer the simplicity of dockcross, but crosstool-ng is definitely worth looking at for more complex builds.