Compiling for multiple processor architectures in Mac OS X 10.5 Leopard

This is probably covered elsewhere in far greater detail, but I thought I’d drop a note here for my own reference more than anything.

If you’re compiling code on Mac OS X 10.5 Leopard from source with standard Unix tools like make, your resulting binary will usually be compiled only for the architecture you’re currently running on. For example, when I first compiled a copy of pwgen for making sets of random passwords, the binary I got was i386 only:

$ file pwgen
pwgen: Mach-O executable i386

But, if you modify the Makefile to include the 4 architectures that Mac OS X (currently) supports:

CFLAGS = -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64
LDFLAGS =  -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64

GCC will be run with those options when compiling the code and creating the final binary, and you’ll get a “fat” binary that supports all the processor architectures:

$ file pwgen
pwgen: Mach-O universal binary with 4 architectures
pwgen (for architecture ppc7400):	Mach-O executable ppc
pwgen (for architecture ppc64):	Mach-O 64-bit executable ppc64
pwgen (for architecture i386):	Mach-O executable i386
pwgen (for architecture x86_64):	Mach-O 64-bit executable x86_64

Note that doing compiling anything on OS X will require that you install the Developer Tools, which you can download from the Apple Developer Connection.


About this entry