Building a Universal binary on OS X with gcc

Recently I’ve been trying to build a C application on OS X. It actually worked first time for me on an Intel MacBook Pro on Leopard (10.5) just via gcc *.c -o [outputfile] … but yesterday it was pointed out that the resulting binary is useless on a G4 Mac Mini.

The nice thing about Apple’s move from PowerPC to Intel chips is that they have this concept of a Universal binary – the same binary file can run on both PPC and Intel. The slightly complicated part is that you have to actually build your binary as Universal, it doesn’t happen automatically.

So it turns out the the corresponding magic-fu is:

gcc -O2 -Wall -force_cpusubtype_ALL -mmacosx-version-min=10.4 -arch i386 -arch ppc *.c -o [outputfile]

At a high level I’m telling the compiler to build for both i386 and ppc architectures. Note that I’ve also set a flag here to specify a minimum OS X level of 10.4 (Tiger).

Of course there are sometimes some coding changes required to support both processor architectures. Apple’s Universal Binary Programming Guidelines should help there.


One response to “Building a Universal binary on OS X with gcc

  1. Pingback: Today, I (hope I) helped people « The lost outpost

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s