nixCraft Poll

Topics

How to compile a 32-bit application using gcc on the 64-bit Linux version

Posted by Vivek Gite [Last updated: December 20, 2006]

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

Luckily gcc man page directed me to -m32 and -m64 option. These options generate code for a 32-bit or 64-bit environments.

=> The 32-bit environment sets int, long and pointer to 32 bits and generates code that runs on any i386 system.

=> 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 as follows
For 32 bit version:
$ gcc -m32 -o output32 hello.c
For 64 bit version :
$ gcc -m64 -o output64 hello.c

And output is :
$ ./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;
}

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. mojo Says:

    I think
    gcc -m32 -o output64 hello.c

    should be
    !
    gcc -m32 -o output32 hello.c

    and

    $ gcc -m64 -o output64 hello.c

    should be

    $ gcc -m64 -o output64 hello.c

  2. nixcraft Says:

    mojo

    Thanks for heads up!

  3. beparas Says:

    its working . thx

  4. vlad Says:

    I got this error trying to compile with -m32 :S
    /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h

    Whats wrong?

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.