I recently have been trying to compile some old C programs that were last used in Solarus OS - ie more than 20 years ago. Computers then were 32 bit.
I first tried a 64 bit compile but there were too many problems. Old poorly written C with casts between integers and pointers has issues moving to 64 bit.
So I decided to compile it in 32 bit mode in my 64 bit computer. I worked in Devuan 5.
I have set down here what I discovered are some of the requirements for a 32bit cross compile. Hope it helps someone .
- use the flag
-m32
on thegcc
compiler. In a Makefile this is normally set with
CFLAGS = -m32
orCCFLAGS = -m32
and it seems to be necessary to also set this flag for the loader
LDFLAGS = -m32
to force the loader to look for 32 bit libraries - setup the architecture. Normally in a 64 bit machine you get this
$ arch
x86_64
but you need to also declare the i386
architecture. Do this with
sudo dpkg --add-architecture i386
dpkg --print-foreign-architectures
i386
After allowing i386 architecture, you need to do
apt-get update
so that your repo database will be updated to contain all the i386 packages
- install any required 32 bit libraries and accompanying -dev packages. For example
apt-get install libxaw7:i386
apt-get install libxaw7-dev:i386
- install the multilib package for gcc ( and fortran if needed)
apt-get install gcc-12-multilib
apt-get install gfortran-12-multilib
- install the cross compile support
apt-get install gcc-12-multilib-x86-64-linux-gnux32
apt-get install gfortran-12-multilib-x86-64-linux-gnux32
- If there are any local libraries in the program files, recompile them in 32 bit mode.
- that’s it try to do
make all
. If you are lucky it may compile
without errors.
In my case , sadly, there were many errors. Some of the most common issues were
- needing to add lines like
#include <stdlib.h>
to the C code. - not able to find
.h
files
Typically you get messages about functions being implicitly defined.
May need to add an include path such as-I/usr/lib/gcc/x86_64-linux-gnu/12/include
to the compiler options - not able to find libraries. Need to install more i386 libraries.
- warnings about casts of pointer to integer of wrong size.
I did eventually get it to compile after 60 attempts.
It runs .
but
it will not open an X11 display window. Some problem calling X_CreateWindow(). That’s my problem.
and
there is some library that it can not find
ERROR: ld.so: object '/usr/lib/x86_64-linux-gnu/libgtk3-nocsd.so.0' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS64): ignored.
because it does not exist for i386 architecture. It does not seem to matter.
So the message is
If you want to try a 32 bit cross compile, be prepared.
Set up the system properly first.
I wasted a lot of time with ill-prepared attempts.