Compile and link 32 bit on a 64 bit machine


My system is openSUSE 10.3 64 bit. I’m trying to compile and link a c++ program to run on a 32 bit system.

This is my makefile:

CXXFLAGS =	-m32 -O2 -g -Wall -fmessage-length=0
OBJS =		hello-world-64.o
TARGET =	hello-world-64

	$(CXX) -o $(TARGET) $(OBJS) $(LIBS)

all:	$(TARGET)

	rm -f $(OBJS) $(TARGET)

As you can see I’m using the -m32 switch to compile for a 32 bit machine. The compile works fine but linking fails with the following error:

mist@lurken:~/hello-world-64> make
g++ -m32 -O2 -g -Wall -fmessage-length=0   -c -o hello-world-64.o hello-world-64.cpp

g++ -o hello-world-64 hello-world-64.o 
collect2: ld terminated with signal 11 [Segmentation fault]
/usr/lib64/gcc/x86_64-suse-linux/4.2.1/../../../../x86_64-suse-linux/bin/ld: i386 ar
chitecture of input file `hello-world-64.o' is incompatible with i386:x86-64 output
make: *** [hello-world-64] Error 1

I notice that linking is done without the -m32 switch, the linker fails when trying to link 32 bit code into a 64 bit binary. But how do I tell the linker to link a 32 bit binary? I’ve looked through the gcc manual but I fail to find any info on linking a 32 bit binary on a 64 bit machine. Can it even be done?

 Regards, Micke.

Yes it can. You have to tell the linker to use 32-bit libraries and the 32-bit startup code by changing the paths. You may have to go right down and control every ld option to make sure this happens. And of course you need to have the libraries and object files for 32-bit.

In Software Management > Filter > Patterns > add the 32-bit runtime Library & you should be set.

Would that be in yast2? I can’t seem to find that under software (OpenSuSe 11.0).
Thanks, David.

Why don’t you just install a 32 bit openSUSE in something like VirtualBox?

Do your main development normally, then build and test in a 32 bit environment?

The thing is, you’re really cross compiling,the whole tool chain has been made so that 32bit binaries will run unaltered on 64bit system, but that means changing library paths and such on 64. You can configure GCC source build, to undo that, but is it really worth the trouble?

Whilst I agree with Ken, I think practically it’d be hard to guarantee the results.

Another option would be to use Online Build Service, which would generate all sorts of packages.

Well you can guarantee the results, it simply won’t work if you link with the wrong library and object file types. But it may take quite a lot of Makefile editing to get there. It may be easier, as you say, to install a virtual 32-bit environment. It all depends on what the OP wants to do.