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.
You have to install correct development packages by
And then it will work:
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...