3

I'm trying to compile a 32 bit version of python on a 64 bit ubuntu system with the following configure command:

CC="gcc -m32" LDFLAGS="-L/lib32 -L/usr/lib32 \ -Lpwd/lib32 -Wl,-rpath,/lib32 \
    -Wl,-rpath,/usr/lib32" \ ./configure --prefix=/opt/pym32

then make, make install. No errors, but it should be something wrong because a "readelf -h python" tells me that python was build as a ELF64 instead.

enzotib
  • 93,831

3 Answers3

6

Use "--build" and "--host".

./configure --help
System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]

You need to use

./configure --build=x86_64-pc-linux-gnu --host=i386-pc-linux-gnu

to compile for 32-bit Linux in a 64-bit Linux system. You still need to add the other ./configure options.

Louis Maddox
  • 223
  • 3
  • 9
  • 1
    tnx a lot! it works perfectly :) – Gabriele B Nov 18 '11 at 08:13
  • "configure: error: readelf for the host is required for cross builds". Using "locate '*-readelf'" gives only "/usr/bin/x86_64-linux-gnu-readelf". I guess I need to find a package to install that includes i686-linux-gnu-readelf" ? – Jonathan Hartley Jan 08 '16 at 02:30
  • This is how I bypassed the readelf error: sudo apt-get install binutils-multiarch, sudo ln -s /usr/bin/readelf /usr/bin/i386-linux-gnu-readelf And then ./configure --build=x86_64-linux-gnu --host=i386-linux-gnu ... – Alex Che Jul 25 '19 at 21:09
2

This is how I managed to build and side-install 32-bit Python 3.5.2 on Ubuntu 16, having 64-bit version already installed by apt:

# Some prerequisites:
sudo apt install libffi-dev:i386
sudo apt install libssl1.0.0:i386
sudo apt install libssl-dev:i386
sudo apt-get install libncurses-dev:i386 libreadline-dev:i386 # for command history to work in Python terminal
sudo apt-get install binutils-multiarch
sudo ln -s /usr/bin/readelf /usr/bin/i386-linux-gnu-readelf
# Going to be installed to /opt/python-3.5.2-i386
ac_cv_file__dev_ptc=no ac_cv_file__dev_ptmx=yes \
CFLAGS=-m32 CXXFLAGS=-m32 \
LDFLAGS="-m32 -L/lib32 -L/usr/lib32 -L/lib/i386-linux-gnu -L/usr/lib/i386-linux-gnu -Wl,-rpath,/lib32 -Wl,-rpath,/usr/lib32 -Wl,-rpath,/lib/i386-linux-gnu -Wl,-rpath,/usr/lib/i386-linux-gnu" \
./configure --build=x86_64-linux-gnu --host=i386-linux-gnu \
--enable-ipv6 --prefix=/opt/python-3.5.2-i386 --with-system-ffi
make
sudo make install
# The following may be needed to build some additional extensions, which are statically 
# built-in in existing python3 intallation on host and won't be built otherwise
sudo PATH=/opt/python-3.5.2-i386/bin:$PATH make
sudo PATH=/opt/python-3.5.2-i386/bin:$PATH make install

After that:

~$ python3 -V
Python 3.5.2
~$ python3 -c 'import struct; print(struct.calcsize("P") * 8)'
64
~$ /opt/python-3.5.2-i386/bin/python3 -V
Python 3.5.2
~$ /opt/python-3.5.2-i386/bin/python3 -c 'import struct; print(struct.calcsize("P") * 8)'
32
Alex Che
  • 121
0

If you want the compile to be debuged, you should post (in a pastebin) your verbose output from gcc. Otherwise, it's impossible to know.

The suggested solution of using chroot may help you and Mikko kindly offers a link to guide users in how to use chroot for 32bit python on a 64bit machine: