How to compile a 32-bit binary on a 64-bit linux machines without touching the CFLAGS environment variable

Lothar picture Lothar · Nov 4, 2009 · Viewed 23.6k times · Source

The solution in

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

is not possible because i use 3rd party software and other stuff which is using the CFLAGS variable already. And patching their makefiles is not allowed - says my boss.

So i have to find another way to enforce it. Maybe some magic with symbolic links redirecting a call to gcc to a 32bit version of the compiler (hoping that the default target is not detected dynamically).

Answer

jvasak picture jvasak · Nov 4, 2009

You are not allowed to change CFLAGS in your environment, but is there any reason you are cannot override it temporarily for the build?

For an autotool-based package, I would first try:

CFLAGS="-m32" ./configure [opts]
make
make install

A well-written configure.ac file should respect your CFLAGS variable and append to it, without requiring you to modify the package source.

Update

Assuming, then, that you can't redefine your CC variable, either, I would play some path tricks. Make a shell script in ${HOME}/gcc32 called gcc with the following (untested):

#!/bin/sh
/usr/bin/gcc -m32 "$@"

Then prepend this to your path when you want to build 32-bit:

export PATH=${HOME}/gcc32:${PATH}

Obvious modifications will support g++, icc, or any other compiler.