How to build 32 bit binaries on 64 bit architecture machine ?

If we have built our application in 32 bit architecture and if we have to move to 64 bit architecture, following two options can be used
1. compile 32 bit version of application in 64 bit architecure
2. port the application to 64 bit architecture.

1. For compiling 32 bit binaries in 64 bit architecture, -m32 option has to be used during compilation
eg: cc -m32 -o

This will build 32 bit binary.

Consider the below example program
#include
int main()
{
long lA;

printf(“Size of long is %i bytes long!\n”, sizeof(lA));
return 0;
}

save the code in a file, say long.c

Following is the output on 32 bit architecture.

$uname -a
Linux lxbld6.india.hp.com 2.4.25 #2 Wed May 26 12:07:17 IST 2004 i686 unknown
$cc -o long_32 long.c
$./long_32
Long int size is 4 bytes long!

Using -m32 flag and building 32 bit binary, we will be reducing the effort required to port the application to 64 bit architecture.

To compile the application in 64 bit architecture for 64 bit binary, -m32 flag should not be used i.e it’s a normal compilation.
eg: cc -o

Again , consider the same example above

$uname -a
Linux lxbld5 2.6.5-7.97-default #1 Fri Jul 2 14:21:59 UTC 2004 x86_64 x86_64 x86_64 GNU/Linux
$cc -m32 -o long_32 long.c
$file long_32
long_32: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped
$./long_32
Long int size is 4 bytes long!
$cc -o long_64 long.c
$file long_64
long_64: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.1, dynamically linked (uses shared libs), not stripped
$./long_64
Long int size is 8 bytes long!

As can be seen, on a 64 bit architecture (x86_64) machine, size of long in 32 bit binary is 4 bytes and 8 bytes in 64 bit binary.

I will continue the discussion further on this in my next writting.

Advertisements
This entry was posted in C Section. Bookmark the permalink.

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