Universal Hello World

December 12, 2006

I’ve been struggling with building universal (fat) binaries on OS X for quite a while now. Finally decided to sit down and figure this out. In a situation with many variables, it’s often good to stop and try to “crystallise” the problem at hand. What better way to do this than with a Hello World - if that won’t compile and link, then why should anything else, right?

#include <stdio.h>



int main (void)

{

        printf ("Hello, world!\n");

        return 0;

}

Then we compile:

> gcc hellow.c -o hellow

> ./hellow

Hello, world!

> file hellow

hellow: Mach-O executable ppc

So far so good. Now let’s see if we can make a fat binary out of this sucker:

> gcc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -arch ppc64 hellow.c -o hellow-fat

> ./hellow-fat

Hello, world!



> file hellow-fat

hellow-fat: Mach-O fat file with 3 architectures

hellow-fat (for architecture ppc):      Mach-O executable ppc

hellow-fat (for architecture i386):     Mach-O executable i386

hellow-fat (for architecture ppc64):    Mach-O 64-bit executable ppc64

Hmm, that wasn’t so bad. I guess here is where the “fat” in “fat binary” comes from:

> ls -lhS hellow*

-rwxr-xr-x   1 filipp  filipp    44K Dec 12 13:45 hellow-fat

-rwxr-xr-x   1 filipp  filipp    13K Dec 12 13:46 hellow

-rw-r--r--   1 filipp  filipp    80B Dec 12 13:21 hellow.c

Well, that turned out to be a useless experiment. At least I know that my cross-compiling support works now…