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

by Vivek Gite · 12 comments

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;
}

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 12 comments… read them below or add one }

1 mojo 12.20.06 at 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

2 nixcraft 12.20.06 at 12:18 pm

mojo

Thanks for heads up!

3 beparas 06.01.07 at 12:07 pm

its working . thx

4 vlad 05.19.08 at 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?

5 Amit 07.17.08 at 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.

6 martin 08.17.08 at 11:19 am

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

7 Tim 05.17.09 at 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.

8 Steven 06.15.09 at 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 …

9 Highwind 08.21.09 at 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?

10 Highwind 08.21.09 at 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.

11 Atie 09.10.09 at 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

12 Sudarsun Santhiappan 10.08.09 at 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

Leave a Comment

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

Previous post:

Next post: