views:

5373

answers:

4

Is it possible to compile a project in 32-bit with cmake and gcc on a 64-bit system? It probably is, but how do I do it?

When I tried it the "ignorant" way, without setting any parameters/flags/etc, just setting LD_LIBRARY_PATH to find the linked libraries in ~/tools/lib it seems to ignore it and only look in subdirectories named lib64.

A push in the right direction would be great. Thanks!

+10  A: 
export CFLAGS=-m32
caf
I see. Such an easy solution...
dala
I guess this will work in combination with cmake?
dala
It should do. You could also modify the cmake script to create a 32 bit target - it would just add `-m32` to the `CFLAGS`, probably by setting `CMAKE_REQUIRED_FLAGS`.
caf
A: 

One way is to setup a chroot environment. Debian has a number of tools for that, for example debootstrap

Dirk Eddelbuettel
Feels a bit extreme to setup a chroot environment just to build 32-bit apps, doesn't it? Any particular reason why you recommend that?
Fredrik
How does that help, exactly?
Hasturkun
It gives you a complete environment in which to also run code. We use that to build (and run) full 32 bit binaries on 64 bit hosts -- sometimes you only get 32 bit builds of third party libraries.For Debian work, we use it to build 32 bit packages on 64 bit hosts.
Dirk Eddelbuettel
I have never experienced any problems what so ever building and running full 32-bit binaries on neither linux, Solaris nor any other 64-bit platform. But I am not using Debian much.
Fredrik
Frederik, do you also deploy them in 32 bit on the 64 bit build host?
Dirk Eddelbuettel
@Dirk: the 32 bit binaries work on both 32 and 64 bit machines (of course), the 64 bit binaries only works on 64 bit machines. It doesn't matter if it is a customer machine or a build host. I honestly don't see where the problem would be unless it is kernel modules you are building.
Fredrik
@Dirk: I think I just understood your issue... You want to deploy your 32-bit apps in something like /usr/bin just like you would have done on a pure 32-bit machine? In my world that's just bad, I need things to be able to coexist with previous versions and other architectures so that is not a problem I ever face.
Fredrik
@Fredrik: I don't deploy any anything in /usr/bin which doesn't come from the distribution.
AProgrammer
+9  A: 
$ gcc test.c -o testc
$ file testc
testc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ldd testc 
    linux-vdso.so.1 =>  (0x00007fff227ff000)
    libc.so.6 => /lib64/libc.so.6 (0x000000391f000000)
    /lib64/ld-linux-x86-64.so.2 (0x000000391ec00000)
$ gcc -m32 test.c -o testc
$ file testc
testc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ldd testc
    linux-gate.so.1 =>  (0x009aa000)
    libc.so.6 => /lib/libc.so.6 (0x00780000)
    /lib/ld-linux.so.2 (0x0075b000)

In short: use the -m32 flag to compile a 32-bit binary.

Also, make sure that you have the 32-bit versions of all required libraries installed (in my case all I needed on Fedora was glibc-devel.i386)

andri
Great thanks! Yes, I do have 32-bit versions of the dependencies.
dala
A: 

In later versions of CMake, one way to do it on each target is:

set_target_properties(MyTarget PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

I don't know of a way to do it globally.

Nathan Monteleone