≡ Menu

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

I had 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 a CentOS/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

# Additional correction by Braden B and others; Editing by VG - log #

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

Comments on this entry are closed.

  • mojo December 20, 2006, 12:15 pm

    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

  • nixCraft December 20, 2006, 12:18 pm

    mojo

    Thanks for heads up!

  • beparas June 1, 2007, 12:07 pm

    its working . thx

  • vlad May 19, 2008, 5:27 pm

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

    Whats wrong?

  • Amit July 17, 2008, 12:02 pm

    How can I build x64 target. using -m64 gives error. how can I download the libraries for 64 bit. remember I am using ubuntu and gcc 4.1.2.

    looks like by default gcc compiles x86.

  • martin August 17, 2008, 11:19 am

    vlad – you need to install gcc multilib for your distribution – it should then compile O.K.

  • Tim May 17, 2009, 5:50 pm

    Any ideas how to convince the average ./configure to compile in 32 bit mode? I’m trying to compile pcsx2 (a PS2 emulator) in 32 bit mode because apparently it works better. I did

    export CFLAGS=-m32
    export CXXFLAGS=-m32

    beforehand, and building stuff works. However the problem is that configure correctly realises I’m using a 64 bit processor and set lots of #defines and other things (such as choosing the x86-64 version of an assembly file). This messes up the build. God I hate autotools.

  • Steven June 15, 2009, 4:15 pm

    Try giving build info to autoconf.
    for a one line solution,
    $> env CFLAGS=”-m32″ LDFLAGS=”-m32″ ./configure –build=i686-unknown-linux-gnu …

  • Highwind August 21, 2009, 1:26 am

    I get a similar error as Vlad:
    In file included from /usr/include/features.h:352,
    from /usr/include/stdio.h:28,
    from slu_util.h:4,
    from slu_sdefs.h:28,
    from sgssv.c:9:
    /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file or directory

    Anyone?

    • Franco November 3, 2012, 8:50 pm

      try
      on suse SYSTEM:
      sudo zypper install glibc-devel.i686

      or, on other systems:
      sudo yum install glibc-devel.i686

      this is going to install the 32 bit version of some libraries

  • Highwind August 21, 2009, 1:32 am

    I found this:
    you might want to install glibc-devel.i386 (even if you’re on x64) to fix this problem
    and this:
    On ubuntu 64-bit version, solution is:
    sudo apt-get install g++-multilib

    hope that will solve it.

    • Vamsi May 3, 2011, 2:37 pm

      This works best!!!

    • Ramon October 6, 2012, 6:07 pm

      Thanks..

    • Ankit Mondal January 14, 2015, 12:01 pm

      Thanks, this 1-line command worked.

  • Atie September 10, 2009, 5:29 pm

    I faced this error when using ” gcc -m32 -o de ‘/home/atieh/Desktop/debug.o” of course my program is not only in C languge , it ‘s embbeded with assembly.
    here is the error:
    skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.3.3/libgcc.a when searching for -lgcc
    /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.3.3/libgcc.a when searching for -lgcc
    /usr/bin/ld: cannot find -lgcc
    collect2: ld returned 1 exit status

  • Sudarsun Santhiappan October 8, 2009, 1:10 pm

    Make sure you have installed the following packages as well:-
    1. libstdc++.i386
    2. libgcc.i386
    3. glibc.i386
    4. glibc-devel.i386

  • jonywalker800 March 12, 2010, 5:23 am

    Thanks this is what i am looking for

  • bknysz March 31, 2010, 12:12 pm

    I’m trying to compile a 32-bit app on 64-bit debian. One of the libraries I need is libelf. The link is failing. Any ideas? I’ve installed ia32-libs, and am using the -m32 compiler switch. Thanks for any help you can give me.

  • Anonymous May 4, 2010, 3:46 pm

    you don’t need a variable. sizeof(long) works fine, too ;)

  • Anonymous June 23, 2010, 12:06 pm

    @bknysz: It’s been a while, but did you find a solution for libelf issue? I am running into the same problem here …

    • bknysz June 23, 2010, 3:41 pm

      If I remember correctly, I just manually copied the 32-bit version into the directory
      /emul/ia32-linux/usr/lib/libelf*

  • felipe1982 November 15, 2010, 6:03 am

    On openSUSE, I fixed the compilation problems by installing
    gcc-c++-32bit
    and all dependencies. now the problem is solved. It will automatically select gcc45, or, the latest gcc.

  • Alex May 21, 2011, 5:08 am

    i have a oriented file(tmp.o) compiled on 32-bit ,and i want to compile it with “my.o” on 64-bit can i use that command???

  • Jiabao Wan June 27, 2011, 3:34 pm

    Thanks ! Thanks! Thanks!
    I searched the entire Baidu(in china), did not find the answer!,This is where the original!

    No one can give me the answer,but the teacher is here:
    Make sure you have installed the following packages as well:-
    1. libstdc++.i386
    2. libgcc.i386
    3. glibc.i386
    4. glibc-devel.i386

  • Jumbo November 18, 2011, 9:38 pm

    Hi should these be installed on the 32 bit or 64 bit?
    1. libstdc++.i386
    2. libgcc.i386
    3. glibc.i386
    4. glibc-devel.i386

  • Ali B Mahmood April 1, 2012, 9:17 am

    Dear Sir\\
    My MAC is 64-bit and I would like to install the following libraries
    1-gawk 2-gmp 3-mpfr 4- mpc 5- libtool 6- gcc 7- hydra 8- mpich2 9- netcdf 10- paralel-netcdf
    So, what is instructions for installation ,Is it as the same instructions if by MAC 32-Bit
    or not
    Regards
    ALI B Mahmoos

  • vir June 27, 2012, 4:16 pm

    under ubuntu you could check your OS by CLI as uname -a. If it returns i686 it is 32 bit OS or if x86_64 then it is 64 bit OS

  • me November 25, 2013, 4:42 pm

    thank you for the gcc command … it is good … BUT

    no, it is NOT working … EVER !

    the PROPER documentation SHOULD INCLUDE also the proper libraries for compiling with the different platform, otherwise this is TOTALLY USELESS, it will ALWAYS GIVE YOU MISSING LIBS ERRORS !

    now, you may think that i have a too loud voice, but i actually try to help your tutorial, also i managed to fix it on my system (mint/ubuntu/debian i used: apt-get install libc6-dev-i386 and managed to get x86 exec on a x64 system)

  • gnu assembler November 11, 2014, 4:12 am

    as –32 -o junk.o junk.s