How to compile 32-bit executable on 64 bit system

Note: I looked through some of the suggested "similar questions" and didn't find anything that looked conclusive. Plus, it seems most of them are 6 years old (from 2014), so I'm hoping for something more up-to-date (and more likely to "just work").

I have a 64 bit Ubuntu system that works fine. I would like to be able to build a 32 bit version of, say, "hello, world". This is mostly an academic pursuit, but it would be convenient to get it working. It would be nice if compiling with "-m32" would "just work", but it doesn't. Worse, my memory is that this used to "just work" (in an older version of 64 bit Linux), but no longer works.

Observe:

$ cat hello.c
#include <stdio.h>
int main() { puts("hello, world"); return 0; }
$ gcc -m32 hello.c
In file included from hello.c:1:0:
/usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory #include <bits/libc-header-start.h> ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
$

A bit of poking around suggests that installing these packages (see below) might help, so I did:

apt-get install libc6-dev-i386-x32-cross

which installed the following:

The following NEW packages will be installed:
libc6-dev-i386-x32-cross libc6-dev-x32-cross libc6-i386-x32-cross libc6-x32-cross linux-libc-dev-x32-cross

After that, with a little bit of fudging, I was able to get it to compile, but not link. The link phase gives these error msgs:

/usr/bin/ld: cannot find Scrt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status

And I can't get any farther than that.

So, any advice will be appreciated.

4

2 Answers

You have to install correct development packages by

sudo apt-get install build-essential libc6-dev-i386

And then it will work:

$ gcc hello.c -o hello32 -m32
$ file hello32
hello32: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=444fafde1d2281a8af74adc452a8db046df1276e, not stripped

Yup - it turns out all you have to do is install libc6-dev-i386 - then everything works.

Thanks for the tip!

Note: There is a lot of confusing and wrong information on the Internet...

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like