- Linking with 32bit libraries under linux 64bit
- Force gcc to compile 32 bit programs on 64 bit platform
- 3 Answers 3
- How do I run 32-bit programs on a 64-bit Debian/Ubuntu?
- 3 Answers 3
- For current releases
- For old releases
- Schroot
- Introduction
- Set up schroot
- Install the new distribution
- Services in the chroot
- Populate the new system
- Going further
- Virtual machine
- How to Compile 32-bit Apps on 64-bit Ubuntu?
- 3 Answers 3
Linking with 32bit libraries under linux 64bit
Let me explain. I’m using webots in combination with aldebaran SDK. My operating system is Debian Squeeze amd64. Webots (64bit) will not work with aldebaran SDK because their libraries are compiled for 32bit. I do not have the source of the libs to recompile in 64bit. While trying to compile the default nao controller under webots, i get the following error:
g++ -o naoqi_for_webots
naoqi_for_webots.o naoproxy.o -L»/usr/local/webots/lib» -lController -L»/home/alex/naoqi-sdk-1.10.44-linux/lib» -lnaoqiclient /usr/bin/ld: skipping incompatible /home/vor73x/naoqi-sdk-1.10.44-linux/lib/libnaoqiclient.so when searching for -lnaoqiclient /usr/bin/ld: cannot find -lnaoqiclient
libnaoqiclient.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, not stripped
I have downloaded and installed the 32bit version of webots, which links fine with the libnaoqiclient.so but will not link with other webots libraries (libController.so) where again, ld complains about incompatible type. Can I link using webots 64bit with the 32bit aldebaran sdk ? Can I link using webots 32bit with the 32bit aldebaran ? (I should, but I still get errors). How can I specify to ld (or through Makefile even better) that the library is 32bits ? Or in the case of using webots 32bit how can I specify that I want a 32bit binary ? I do not care if my binary is 32 or 64bit, I do not care if I use the 32 or 64bit version of webots, all I want is to be able to compile the controllers.
Force gcc to compile 32 bit programs on 64 bit platform
I’ve got a proprietary program that I’m trying to use on a 64 bit system. When I launch the setup it works ok, but after it tries to update itself and compile some modules and it fails to load them. I’m suspecting it’s because it’s using gcc and gcc tries to compile them for a 64 bit system and therefore this program cannot use these modules. Is there any way (some environmental variables or something like that) to force gcc to do everything for a 32 bit platform. Would a 32 bit chroot work?
3 Answers 3
You need to make GCC use the -m32 flag.
You could try writing a simple shell script to your $PATH and call it gcc (make sure you don’t overwrite the original gcc, and make sure the new script comes earlier in $PATH , and that it uses the full path to GCC.
I think the code you need is just something like /bin/gcc -m32 $* depending on your shell (the $* is there to include all arguments, although it might be something else – very important!)
You’ll also need the 32bit C library, as well as 32 bit versions of whatever external libraries the program links against in some cases.
You may get a 32-bit binary by applying Alan Pearce’s method, but you may also get errors as follows:
fatal error: bits/predefs.h: No such file or directory
If this is the case and if you have apt-get, just install gcc-multilib
sudo apt-get install gcc-multilib
For any code that you compile directly using gcc / g++ , you will need to add -m32 option to the compilation command line, simply edit your CFLAGS , CXXFLAGS and LDFLAGS variables in your Makefile .
For any 3rd party code you might be using you must make sure when you build it to configure it for cross compilation. Run ./configure —help and see which option are available. In most cases you can provide your CFLAGS , CXXFLAGS and LDFLAGS variables to the configure script. You also might need to add —build and —host to the configure script so you end up with something like
./configure CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32 --build=x86_64-pc-linux-gnu --host=i686-pc-linux-gnu
If compilation fails this probably means that you need to install some 32 bit development packages on your 64 bit machine
How do I run 32-bit programs on a 64-bit Debian/Ubuntu?
I have a 64-bit (amd64 a.k.a. x86_64) Debian or Ubuntu installation. I need to run 32-bit (i386/i686) programs occasionally, or to compile programs for a 32-bit system. How can I do this with a minimum of fuss? Bonus: what if I want to run or test with an older or newer release of the distribution?
3 Answers 3
For current releases
Current Debian and Ubuntu have multiarch support: You can mix x86_32 (i386) and x86_64 (amd64) packages on the same system in a straightforward way. This is known as multiarch support — see Ubuntu or Debian wiki more information.
See warl0ck’s answer for a simple, up-to-date answer.
For old releases
In older releases, Debian and Ubuntu ship with a number of 32-bit libraries on amd64. Install the ia32-libs package to have a basic set of 32-bit libraries, and possibly other packages that depend on this one. Your 32-bit executables should simply run if you have all the required libraries. For development, install gcc-multilib , and again possibly other packages that depend on it such as g++-multilib . You may find binutils-multiarch useful as well, and ia32-libs-dev on Debian. Pass the -m32 option to gcc to compile for ix86.
Note that uname -m will still show x64_64 if you’re running a 64-bit kernel, regardless of what 32-bit user mode components you have installed. Schroot described below takes care of this.
Schroot
This section is a guide to installing a Debian-like distribution “inside” another Linux distribution. It is worded in terms of installing a 32-bit Ubuntu inside a 64-bit Ubuntu, but should apply with minor modifications to other situations, such as installing Debian unstable inside Debian stable or vice versa.
Introduction
The idea is to install an alternate distribution in a subtree and run from that. You can install a 32-bit system on a 64-bit system that way, or a different release of your distribution, or a testing environment with different sets of packages installed.
The chroot command and system call starts a process with a view of the filesystem that’s restricted to a subtree of the directory tree. Debian and Ubuntu ship schroot, a utility that wraps around this feature to create a more usable sub-environment.
Install the schroot package (Debian) and the debootstrap package (Debian). Debootstrap is only needed for the installation of the alternate distribution and can be removed afterwards.
Set up schroot
This example describes how to set up a 32-bit Ubuntu 10.04LTS (lucid lynx) alternate environment. A similar setup should work with other releases of Debian and Ubuntu. Create a file /etc/schroot/chroot.d/lucid32 with the following contents:
[lucid32] description=Ubuntu 10.04LTS 32-bit directory=/32 type=directory personality=linux32 users=yourusername groups=users,admin
The line directory=/32 tells schroot where we’ll put the files of the 32-bit installation. The line username=yourusername says the user yourusername will be allowed to use the schroot. The line groups=users,admin says that users in either group will be allowed to use the schroot; you can also put a users=… directive.
Install the new distribution
Create the directory and start populating it with debootstrap. Debootstrap downloads and installs a core set of packages for the specified distribution and architecture.
mkdir /32 debootstrap --arch i386 lucid /32 http://archive.ubuntu.com/ubuntu
You almost have a working system already; what follows is minor enhancements. Schroot automatically overwrites several files in /32/etc when you run it, in particular the DNS configuration in /etc/resolv.conf and the user database in /etc/passwd and other files (this can be overridden, see the documentation). There are a few more files you may want to copy manually once and for all:
cp -p /etc/apt/apt.conf /32/etc/apt/ # for proxy settings cp -p /etc/apt/sources.list /32/etc/apt/ # for universe, security, etc cp -p /etc/environment /32/etc/ # for proxy and locale settings cp -p /etc/sudoers /32/etc/ # for custom sudo settings
There won’t be a file /etc/mtab or /etc/fstab in the chroot. I don’t recommend using the mount command manually in the chroot, do it from outside. But do create a good-enough /etc/mtab to make commands such as df work reasonably.
ln -s /proc/mounts /32/etc/mtab
With the directory type, schroot will perform bind mounts of a number of directories, i.e. those directories will be shared with the parent installation: /proc , /dev , /home , /tmp .
Services in the chroot
As described here, a schroot is not suitable for running daemons. Programs in the schroot will be killed when you exit the schroot. Use a “plain” schroot instead of a “directory” schroot if you want it to be more permanent, and set up permanent bind mounts in /etc/fstab on the parent installation.
On Debian and Ubuntu, services start automatically on installation. To avoid this (which could disrupt the services running outside the chroot, in particular because network ports are shared), establish a policy of not running services in the chroot. Put the following script as /32/usr/sbin/policy-rc.d and make it executable ( chmod a+rx /32/usr/sbin/policy-rc.d ).
#!/bin/sh ## Don't start any service if running in a chroot. ## See /usr/share/doc/sysv-rc/README.policy-rc.d.gz if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then exit 101 fi
Populate the new system
Now we can start using the chroot. You’ll want to install a few more packages at this point.
schroot -c lucid32 sudo apt-get update apt-get install lsb-core nano .
You may need to generate a few locales, e.g.
locale-gen en_US en_US.utf8
If the schroot is for an older release of Ubuntu such as 8.04 (hardy), note that the package ubuntu-standard pulls in an MTA. Select nullmailer instead of the default postfix (you may want your chroot to send mail but you definitely don’t want it to receive any).
Going further
For more information, see the schroot manual, the schroot FAQ and the schroot.conf manual. Schroot is part of the Debian autobuilder (buildd) project. There may be additional useful tips on the Ubuntu community page about debootstrap.
Virtual machine
If you need complete isolation of the alternate environment, use a virtual machine such as KVM (qemu-kvm ) or VirtualBox.
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?
3 Answers 3
This is known to work on Ubuntu 16.04 through 22.04:
sudo apt install gcc-multilib g++-multilib
Then a minimal hello world:
compiles without warning with:
gcc -m32 -ggdb3 -O0 -pedantic-errors -std=c89 \ -Wall -Wextra -pedantic -o main.out main.c
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
but fails on an x86_64 executable with:
./main.out: Invalid ELF image for this architecture
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:
We are able to run 32-bit programs directly on 64-bit Ubuntu because the Ubuntu kernel is configured with:
grep CONFIG_IA32_EMULATION "/boot/config-$(uname -r)"
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 ?
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, and certainly it has no libraries in them.
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).
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
Note that the -m32 flag is specific to 32-bit x86. You need different flags to compile for 32-bit ARM, RISC-V, etc.
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.