I'm trying to compile a 32-bit C application on Ubuntu Server 12.04 LTS 64-bit using gcc 4.8. I'm getting linker error messages about incompatible libraries and skipping -lgcc
. What do I need to do to get 32 bit apps compiled and linked?

- 8,420
- 10
- 51
- 68
-
A related link: https://stackoverflow.com/questions/58654288/how-to-make-compiler-generate-a-elf32-x86-64-format-object-file – smwikipedia Nov 04 '19 at 07:12
3 Answers
This is known to work on Ubuntu 16.04 through 22.04:
sudo apt install gcc-multilib g++-multilib
Then a minimal hello world:
main.c
#include <stdio.h>
int main(void) {
puts("hello world");
return 0;
}
compiles without warning with:
gcc -m32 -ggdb3 -O0 -pedantic-errors -std=c89 \
-Wall -Wextra -pedantic -o main.out main.c
And
./main.out
outputs:
hello world
And:
file main.out
says:
main.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=87c87a83878ce7e7d23b6236e4286bf1daf59033, not stripped
and:
qemu-i386 main.out
also gives:
hello world
but fails on an x86_64
executable with:
./main.out: Invalid ELF image for this architecture
Furthermore, I have:
- run the compiled file in a 32 bit VM
- compiled and run an IA-32 C driver + complex IA-32 code
So I think it works :-)
See also: Cannot find crtn.o, linking 32 bit code on 64 bit system
It is a shame that this package conflicts with the cross compilers like gcc-arm-linux-gnueabihf
https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1300211
Running versions of the question:
- https://unix.stackexchange.com/questions/12956/how-do-i-run-32-bit-programs-on-a-64-bit-debian-ubuntu
- https://askubuntu.com/questions/454253/how-to-run-32-bit-app-in-ubuntu-64-bit
We are able to run 32-bit programs directly on 64-bit Ubuntu because the Ubuntu kernel is configured with:
CONFIG_IA32_EMULATION=y
according to:
grep CONFIG_IA32_EMULATION "/boot/config-$(uname -r)"
whose help on the kernel source tree reads:
Include code to run legacy 32-bit programs under a
64-bit kernel. You should likely turn this on, unless you're
100% sure that you don't have any 32-bit programs left.
This is in turn possible because x86 64 bit CPUs have a mode to run 32-bit programs that the Linux kernel uses.
TODO: what options does gcc-multilib
get compiled differently than gcc
?

- 3,268
- 4
- 31
- 51

- 347,512
- 102
- 1,199
- 985
-
7
-
1As an aside, for C++ projects `g++-multilib` package should be installed. – E. Vakili May 10 '20 at 08:37
-
Doesn't work in podman/docker container with Ubuntu 18.04. As a matter of fact, I don't see why would it ever work, because the mentioned gcc-multilib packages [barely has any files](https://packages.ubuntu.com/bionic/amd64/gcc-multilib/filelist), and certainly it has no libraries in them. – Hi-Angel Sep 14 '21 at 21:47
-
So, what helped for me in a docker/podman container with Ubuntu, is to install `lib32gcc-10-dev` *(worth noting, the 10 version in my case is from PPA; without PPA it would be a lower version)*. – Hi-Angel Sep 14 '21 at 21:55
To get Ubuntu Server 12.04 LTS 64-bit to compile gcc 4.8 32-bit programs, you'll need to do two things.
Make sure all the 32-bit gcc 4.8 development tools are completely installed:
sudo apt-get install lib32gcc-4.8-dev
Compile programs using the -m32 flag
gcc pgm.c -m32 -o pgm

- 8,420
- 10
- 51
- 68
-
1Note that the `-m32` flag is specific to 32-bit x86. You need different flags to compile for 32-bit ARM, RISC-V, etc. – Aaron Franke May 31 '22 at 19:34
Multiarch installation is supported by adding the architecture information to the package names you want to install (instead of installing these packages using alternative names, which might or might not be available).
See this answer for more information on (modern) multiarch installations.
In your case you'd be better off installing the 32bit gcc and libc:
sudo apt-get install libc6-dev:i386 gcc:i386
It will install the 32-bit libc development and gcc packages, and all depending packages (all 32bit versions), next to your 64-bit installation without breaking it.
-
1Be aware that 64-bit gcc will be uninstalled if existing (though the libraries will remain) – Paul Stelian Jan 17 '18 at 15:33