views:

3558

answers:

3

I have trouble understanding the gcc compiler provided by OSX 10.6 snow leopard, mainly because of my lack of experience with 64 bits environments.

$ cat >foo.c
main() {}
$ gcc foo.c -o foo
$ file foo
foo: Mach-O 64-bit executable x86_64
$ lipo -detailed_info foo
input file foo is not a fat file
Non-fat file: foo is architecture: x86_64

However, my architecture is seen as an intel i386 type (I have one of the latest Intel Core2 duo MacBook)

$ arch
i386

and the compiler targets i686-apple-darwin10

$ gcc --version 
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646)

Of course, if I compile 32 bits I get a 32 bit executable.

$ gcc -m32 foo.c -o foo
$ file foo
foo: Mach-O executable i386

but I don't get the big picture. The default setup for the compiler is to produce x86_64 executables, even if I have arch saying I have a 32 bit machine (why? Core2 is 64); even if (I guess) I am running a 32 bit kernel; even if I have a compiler targeting the i686-apple-darwin platform. Why? How can they run ? Should I compile 64 or 32 ?

This question is due to my attempt to compile gcc 4.2.3 on the mac, but I am having a bunch of issues with gmp, mpfr and libiberty getting (in some cases) compiled for x86_64. Should I compile everything x86_64 ? If so, what's the target (not i686-apple-darwin10 I guess)?

Thanks for the help

+5  A: 

The default compiler on Snow Leopard is gcc4.2, and its default architecture is x86_64. The typical way to build Mac software is to build multiple architectures in separate passes, then use lipo to combine the results. (lipo only compiles single-arch files into a multiple-arch file, or strips archs out of a multi-arch file. It has no utility on single-arch files, as you discovered.)

The bitness of the compiler has nothing to do with anything. You can build 32-bit binaries with a 64-bit compiler, and vice versa. (What you think is the "target" of the compiler is actually its executable, which is different.)

The bitness of the kernel has nothing to do with anything. You can build and run 64-bit binaries when booted on a 32-bit kernel, and vice versa.

What matters is when you link, whether you have the appropriate architectures for linking. You can't link 32-bit builds against 64-bit binaries or vice versa. So the important thing is to see what the architectures of your link libraries are, make sure they're coherent, then build your binary of the same architecture so you can link against the libraries you have.

cdespinosa
The problem I see right now is that my mpfr and gmp libs are compiled for x86_64, but when I compile gcc, it fails to link because apparently the compiler builds a i386-apple-darwin10.0.0 compiler, which fails to link to my libs "ld: warning: /blabla/libmpfr.dylib, file is not of required architecture. I am now trying to compile gcc with the --build/target/host as i686-apple-darwin10. crossing fingers, but it's clear that I am not able to create compatible objects for some reason.
Stefano Borini
No chance. For some reason, compiling the compiler always produce a lot of i386 stuff, which of course cannot link against the x86_64 gmp and mpfr libraries. I'm really puzzled.
Stefano Borini
Well, you either need to build your compiler for -arch x86_64 to link with your gmp and mpfr libs, or get i386 versions of those libs.
cdespinosa
A: 

The real question is... how did you get a 32-bit version of OSX? I wasn't aware that Snow Leopard had a 32-bit version, as all of Apple's Intel chips are Core 2 or Xeon, which support the x86_64 architecture.

Oh, and Snow Leopard only works on Intel chips.

Edit: Apparently Snow Leopard starts in 32-bit mode.

R. Bemrose
I assumed the Core 2 duo was seen as a 32 bit chip because arch answers i386, but I am apparently wrong. Still, why then arch answers i386 ?
Stefano Borini
Peter M
Must be for compatibility reasons then.
R. Bemrose
SnowLeopard runs 32-bit system software and applications on 32-bit Intel machines, and 64-bit system software and 32-bit and 64-bit applications on 64-bit Intel machines. The desktop version of Snow Leopard boots a 32-bit kernel for reasons of kext and driver compatibility, but all user space runs 64-bit. Mac OS X Server boots into a 64-bit kernel. Core 2 Duo is a 64-bit machine. Try $ sysctl hw.cpu64bit_capable or sysctl hw.optional.x86_64 to verify that you have a 64-bit cpu. arch will always show i386 on Intel hardware in both Leopard and Snow Leopard.
cdespinosa
I would remove this answer, but I think the comments to it could be useful to other users.
R. Bemrose
Apple also shipped Core 1 Duo and Solo processors in some computers, which of course are 32 bit.
Coxy
@coxymla: Reference? I'm fairly sure that even the Mac Mini started out as a Core 2 Solo and Duo (for the two respective models).
R. Bemrose
+1  A: 

i686-apple-darwin10.0.0 contains an x86_64 folder which is not understood by most versions of autotools. In other words, I'd say that the gcc compiler is unfortunately nothing short of a joke on Snow Leopard. Why you would bundle 32-bit and 64-bit libraries into i686-apple-darwin10.0.0 is beyond me.

$ ls /usr/lib/gcc
i686-apple-darwin10 powerpc-apple-darwin10

You need to change all your autotools configure files to handle looking in 86-darwin* directories and then looking for 64-bit libraries I'd imagine.

As with your system, my mac mini says its i386 even though its obviously using a 64-bit platform, again another mistake since its distributed with 64-bit hardware.

$arch
i386
Eamonn Kenny