9

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

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431

4 Answers4

15

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
  • 20,661
  • 6
  • 33
  • 39
  • 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 Sep 21 '09 at 19:25
  • 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 Sep 21 '09 at 22:28
  • 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 Sep 22 '09 at 04:25
  • I ran into a similar issue, GMP built correctly as x86_64, but other libraries (Nettle, GnuTLS) were defaulting to i386. The Nettle developer has had lots of complaints of this and determined the issue to be in `config.guess`. It seems like, since Leopard & Snow Leopard default to booting the 32-bit kernel, even on 64-bit processors, that it defaults to i386, where GMP and other libs do more specific processor checks and so detect they're 64-bit capable. I'm going to try to get a patch submitted for `config.guess` using `sysctl -n hw.cpu64bit_capable` or the like. – morgant Nov 12 '11 at 13:15
2

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
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
1

Apple toolchains support multiple architectures. If you want to create a fat binary that contains x86 and x86_64 code, then you have to pass the parameters -arch i386 -arch x86_64 to gcc. The compiler will compile your code twice for both platforms in one go.

Adding -arch i386 -arch x86_64 to CFLAGS may allow you to compile gmp, mpfr, and whatnot for multiple archs in one go. Building libusb that way worked for me.

Sven
  • 1,364
  • 2
  • 17
  • 19
0

This answer is wrong, but see comments below

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.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 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 Sep 21 '09 at 17:46
  • Snow Leopard defaults to 32 bit mode on start up. You have to explicitly tell it to run in 64 bit mode either by holding down keys on startup ( "6" & "4" ??) or manually setting a preference – Peter M Sep 21 '09 at 17:55
  • Must be for compatibility reasons then. – Powerlord Sep 21 '09 at 18:56
  • 6
    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 Sep 22 '09 at 04:23
  • 5
    I would remove this answer, but I think the comments to it could be useful to other users. – Powerlord Sep 22 '09 at 18:44
  • Apple also shipped Core 1 Duo and Solo processors in some computers, which of course are 32 bit. – Coxy Oct 29 '09 at 01:19
  • @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). – Powerlord Oct 29 '09 at 13:36