Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 897
Lothar Avatar asked Nov 04 '09 12:11

Lothar


2 Answers

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.

like image 56
jvasak Avatar answered Oct 05 '22 23:10

jvasak


Let's assume that gcc and friends are located in "/usr/bin". Let us also assume that you have a folder called "~/.local/bin" that is in your path. You can create a bash script named "gcc" in "~/.local/bin" like:

#! /bin/bash
/usr/bin/gcc -m32 $@

You can similarly create a "g++" script with the content as follows:

#! /bin/bash
/usr/bin/g++ -m32 $@

Basically, keep doing this for "c++", "cc", "as", "ld", etc. as needed. Make the scripts executable. Make sure that "~/.local/bin" is in your PATH environment variable BEFORE "/usr/bin". If that is the case, then when the makefile invokes the compiler (assuming they used relative paths), your scripts will be invoked, instead.

like image 26
Michael Aaron Safyan Avatar answered Oct 05 '22 22:10

Michael Aaron Safyan