Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Compile 32-bit Apps on 64-bit Ubuntu?

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?

like image 743
BSalita Avatar asked Mar 12 '14 14:03

BSalita


People also ask

How can I run 32-bit programs on 64 bit Ubuntu?

To be able to run a 32-bit program on 64-bit Linux system such as Ubuntu, you need to add i386 architecture and install 3 library packages libc6:i386, libncurses5:i386, and libstdc++6:i386.

How do I compile 32-bit programs on 64 bit gcc?

Now in order to compile with 32-bit gcc, just add a flag -m32 in the command line of compiling the 'C' language program. For instance, to compile a file of geek. c through Linux terminal, you must write the following command with -m32 flag. After that you will be able to compile a 32-bit binary on a 64-bit system.

Does Ubuntu 20.04 support 32-bit applications?

However, with Ubuntu 20.04 there is no support for 32-bit at all.

Does Ubuntu support 32-bit apps?

Ubuntu doesn't provide 32-bit ISO download for its release for the past couple of years. Existing 32-bit Ubuntu users could still upgrade to the newer versions. But in Ubuntu 19.10, there are no 32-bit libraries, software and tools. If you are using a 32-bit Ubuntu 19.04, you cannot upgrade to Ubuntu 19.10.


2 Answers

Ubuntu 16.04

sudo apt-get install gcc-multilib 

For some reason, on Ubuntu 17.04, I also needed to install the version specific one:

sudo apt-get install gcc-6-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?


To get Ubuntu Server 12.04 LTS 64-bit to compile gcc 4.8 32-bit programs, you'll need to do two things.

  1. Make sure all the 32-bit gcc 4.8 development tools are completely installed:

    sudo apt-get install lib32gcc-4.8-dev

  2. Compile programs using the -m32 flag

    gcc pgm.c -m32 -o pgm

like image 32
BSalita Avatar answered Sep 28 '22 23:09

BSalita