27

Trying to compile even the simplest (int main(){}) program with -m32 on a 64-bit system fails:

$ gcc -m32 test.c
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

It seems to be still erroneously searching in the wrong directories when -m32 is specified.

libgcc-4.8-dev:i386 is installed and I've verified that the 32-bit libraries are located in /usr/lib/gcc/i686-linux-gnu/4.8/.

I've tried setting and exporting LD_INCLUDE_PATH and LD_LIBRARY_PATH to no avail.

Vladimir Panteleev
  • 1,173
  • 2
  • 12
  • 21
  • Related: [HowTo Compile a 32-bit Application Using gcc On the 64-bit Linux Version](http://www.cyberciti.biz/tips/compile-32bit-application-using-gcc-64-bit-linux.html) – kenorb Jun 14 '15 at 11:48

4 Answers4

31

First, you need to install "gcc-multilib" and "g++-multilib"

sudo apt-get install gcc-multilib g++-multilib

Then your current command will work,

$ cat hello.c
#include <stdio.h>

int main(int argc, char *argv[]) {
    puts("Hello, World!");
}
$ uname -a
Linux vbox-1404 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ gcc -m32 hello.c
$ ./a.out
Hello, World!
$ file a.out
a.out: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=80bdc95e2941e3ba8d7bb7c1682098f20e77cebc, not stripped
Elliott Frisch
  • 2,998
  • 1
  • 21
  • 22
  • 3
    Note: For C++ programs, `g++-multilib` will also be required. – Vladimir Panteleev Jun 28 '14 at 12:32
  • 1
    Consider also: `libc6-dev-i386`. – kenorb Jun 14 '15 at 11:47
  • Thanks, this just helped me with a cross-compilation issue. I was able to target 64-bit SPARC, but not 32-bit SPARC. I was missing 'gcc-multilib-sparc64-linux-gnu'. – Max Barraclough Feb 05 '20 at 17:53
  • Thanks for the simple hello.c example - I'm trying to build a huge open source stack with a build script that keeps itself entertained for 10 minutes before crashing with this error message. Nice to be able to test the solution without the whole 10 minute wait. – GregHNZ Sep 05 '21 at 21:23
2

This works for me. I'm on Ubuntu 18.04, x86-64.

First, gcc and gcc-multilib must match the version of each other. If you have installed multi versions of gcc on your machine, then you have to specify one version as the priority. For example I choose gcc 4.8:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 51

The greater the last number specified, the more priority it gets. You can check them out by using this command:

update-alternatives --config gcc

Then you have to install the version of gcc-multilib that match the version of gcc:

sudo apt-get install gcc-4.8-multilib

Done.

Thành Vũ
  • 21
  • 2
0

This problem also happens when gcc version doesn't match with the version of gcc-multilib. In this case you should install the correct version of the multilib packages. For example; I was using gcc version 4.6 so I had to install gcc-4.6-multilib and g++-4.6-multilib because default gcc-multilib package was for 4.8.

HeyYO
  • 184
  • 1
  • 7
0

For successfully compiling C code to x86 32 Bit executables in an x86_64 environment you have to install the following extra packages:

sudo apt-get install libc6-dev-i386 gcc-multilib

After a compile call like

$ gcc -m32 test.c -o test

succeeds.

maxschlepzig
  • 3,344
  • 3
  • 29
  • 33