Universal binaries with Netbeans

How to compile a MacOS executable from C++ using the Netbeans IDE.

Universal binaries are one of Apple's clever innovations that paper over the differences between architectures by including several distinct executables within a single file. When run, the OS examines the file and decides which binary to execute. Thus you can transparently support Intel and PowerPC machines.

Strangely, for something so important, the documentation for this feature is uneven. While there is coverage wihtin Apple techdocs, there are outdated and obscure spots. It gets more difficult if you insist on working outside XCode. After trial and error, I've found out how to produce cover Universal binaries from a Netbeans C++ project.

  1. In Netbeans, highlight the project you are compiling and right/control/context click. Select Properties.

  2. In the ensuing dialog, select C++ Compiler.

  3. Select the editor button for Additional options. Enter:

    -force_cpusubtype_ALL -mmacosx-version-min=10.4 -arch ppc -arch i386
    
  4. Apply the changes and compile.

A similar process should work for C programs as well.

While the arch arguments are obvious, neglecting the first two will result in something that looks like a universal binary, but doesn't work like one with the cross compiled target failing. It's unclear to me if mmacosx-version-min can realistically be set to anything earlier, but 10.4 should catch most users anyway. You can check the result with file, and get something like this (where mesa is the executable):

% file mesa mesa: Mach-O universal binary with 2 architectures
mesa (for architecture ppc): Mach-O executable ppc
mesa (for architecture i386): Mach-O executable i386