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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s