Force gcc to compile 32 bit programs on 64 bit platform

[Origin]: http://stackoverflow.com/questions/3501878/force-gcc-to-compile-32-bit-programs-on-64-bit-platform

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?

shareedit

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

shareedit

You’ll also need the 32bit C library, as well as 32 bit versions of whatever external libraries the program links against in some cases. – Tim Post Aug 17 ’10 at 11:12

Indeed. Standard libraries: C: apt-get install gcc-multilib; C++: apt-get install g++-multilib. – Bart Nov 20 ’14 at 17:13

HowTo Compile a 32-bit Application Using gcc On the 64-bit Linux Version

[Origin]: http://www.cyberciti.biz/tips/compile-32bit-application-using-gcc-64-bit-linux.html

Ihad to compile a 32-bit application using GNU gcc on the 64-bit version of Linux.

Luckily, gcc man page directed me to the ‘-m32’ and ‘-m64’ options. These options generate code for 32-bit or 64-bit environments, respectively.

  1. The 32-bit environment sets int, long and pointer to 32 bits and generates code that runs on any i386 system.
  2. The 64-bit environment sets int to 32 bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture.

You can pass -m64 or -m32 options as follows to Gnu gcc

For 32 bit version:

$ gcc -m32 -o output32 hello.c

For 64 bit version :

$ gcc -m64 -o output64 hello.c

Run it as follows:

$ ./output32

Output:

Long int size is 4 bytes long!

Now let us see 64 bit output:
$ ./output64

Long int size is 8 bytes long!

Sample code – hello.c

#include <stdio.h>
int main() {
    long z; printf("Long int size is %i bytes long!\n", sizeof(z)); return 0;
}

A note about glibc-devel on 64 bit systems

You may see an error as follows on 64 bit system when you try to build 32 bit app:

/usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h:

This error message shows up on the 64 bit systems where GCC multilib feature is enabled, and it indicates that 32 bit version of libc is not installed. To fix this problem, on aCentOS/RHEL 5.x type the following yum command:

# yum -y install glibc-devel.i386 libstdc++-devel.i386

To fix this problem, on a CentOS/RHEL 6.x type the following yum command:

# yum -y install glibc-devel.i686 glibc-devel ibstdc++-devel.i686

To fix this problem on a Debian or Ubuntu Linux type the following apt-get command:

$ sudo apt-get install g++-multilib libc6-dev-i386

To fix this problem on a Suse (SLES) / OpenSUSE Linux type the following zypper command:

# zypper in glibc-devel-32bit
See also

How do you use gcc to generate assembly code in Intel syntax?

[Origin]: http://stackoverflow.com/questions/199966/how-do-you-use-gcc-to-generate-assembly-code-in-intel-syntax

The gcc -S option will generate assembly code in AT&T syntax, is there a way to generate files in Intel syntax? Or is there a way to convert between the two?

shareedit

Have you tried this?

gcc -S -masm=intel test.c

Untested, but I found it in this forum where someone claimed it worked for them.

I just tried this on the mac and it failed, so I looked in my man page:

   -masm=dialect
       Output asm instructions using selected dialect.  Supported choices
       are intel or att (the default one).  Darwin does not support intel.

It may work on your platform.

For Mac OSX:

clang++ -S -mllvm --x86-asm-syntax=intel test.cpp

Source: http://stackoverflow.com/a/11957826/950427

shareedit

GCC preprocessor output and compilation in one pass

[Origin]: http://stackoverflow.com/questions/9041381/gcc-preprocessor-output-and-compilation-in-one-pass

Is it possible to generate preprocessor output and compilation in one step with GCC?

Something like:

gcc -E -c main.cc -o main.o

that would generate main.o and main.i

shareedit

Yes.

Look at gcc -save-temps option.

It compiles the source file and saves the result of the preprocessing in a .i file. (It also saves the result of the assembler phase to a .s file).

gcc -save-temps -c main.cc -o main.o

will generate main.o but also main.i and main.s.

main.i is the result of the preprocessing.

shareedit