Develop on Linux 64-bit

From miki
Revision as of 14:31, 17 October 2012 by Mip (talk | contribs) (→‎Building for i386 target on amd64)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Building for i386 target on amd64

This chapter explains how to build 32-bit applications on 64-bit GNU/Linux (Ubuntu, Debian).

References:


Install the basic 32-bit libraries
Install the package ia32-libs, which contains a basic set of 32-bit libraries (only console applications and basic X and GTK applications).
Optionally install also the package ia32-libs-gtk to get some extra 32-bit GTK (GIMP ToolKit) libraries.
sudo apt-get install ia32-libs
sudo apt-get install ia32-libs-gtk      # optional


Compilation and linking
With gcc, use the flag -m32 both for compilation and linking:
gcc -m32 -o output32 hello.c
With ld, link using emulation elf_i386:
ld -m elf_i386 -o output32 hello.o

We can the list of all emulations that ld supports with:

ld -V

# Ubuntu 10.04 32-bit     Ubuntu 12.04 64-bit     Cygwin
#   elf_i386              elf_i386
#   i386linux             i386linux
#   elf_x86_64            elf_x86_64
#                         elf32_x86_64
#   elf_l1om              elf_l1om
#                         elf_k1om
#                                                 i386pe
Note: Note that using gcc -m32 is much more portable.


Find which dynamic libraries are needed with ldd
Find out which dynamic libraries are needed by the executables. If these libraries are not in the basic set of 32-bit libraries installed above, they'll have to be installed manually.
ldd output32


Applications using java (openjdk)

The .java can be compiled with whatever version (32-bit or 64-bit). However native applications targetting the i386 platform must be compiled and linked against the 32-bit versions of the java libraries.

Get the java 32-bit libraries
To get the libraries, the easiest is to copy them from a 32-bit linux machine. Copy the jre/ directory to say /usr/lib32/jre on the 64-bit machine:
#For instance, for java-6-openjdk
sudo mkdir -p /usr/lib32/jvm
sudo cp -r .../java-6-openjdk /usr/lib32/jvm
Alternatively these libraries can be extracted from package (e.g. package openjdk-6-jre-headless, copy directory /usr/lib/jvm/java-6-openjdk)
Compile and link against the 32-bit libraries
#Compile
gcc -m32 -I $(NFC4JAVA_JDKBASE)/include -I $(NFC4JAVA_JDKBASE)/include/linux -o ... -c ...
#Link
gcc -m32 -Wl,-rpath,/usr/lib32/jre/lib/i386/client -L /usr/lib32/jre/lib/i386/client -ljvm -o ... ...

Other solutions

  • Using chroot
Install a complete 32-bit build system in a chroot directory
  • Using a Virtual Environment
Such as VirtualBox or QEmu