Tuesday, March 21, 2006

XCode - Linking Different Static Library Architectures

This had me scratching my head. If you have a static library you want to link into your universal binary, and the library comes in two different files of the same name but in different directories, one for PowerPC and one for Intel i386, then how do you add them both into XCode? Won't the linker find one version first and stop looking and later give an error about missing symbols? Well there are two solutions:

1) Use the lipo utility to join your two static libraries into one universal binary library. This is fine, but you either have to write a script to automate the process or you have to manually run lipo. This is what I did earlier with the gpc runtime library.

2) Make use of the CURRENT_ARCH environment variable. When a universal binary is being built, XCode will set this variable first to "i386", and then later to "ppc". You can use this to set (via XCode's project or target build configuration pane) your library pathnames properly for each half of the build, as in ../mylibs/$(CURRENT_ARCH) which will give a path to your ../mylibs/ppc directory during the PowerPC build and your ../mylibs/i386 directory during the Intel portion. Of course, this requires you arrange your environment in such a fashion. And the directory doesn't even have to be i386/ppc it could be mac_i386/mac_ppc if you used ../mylibs/mac_$(CURRENT_ARCH)

The same thing works for header paths.

One trick I figured out today, if you need to know an XCode environment variable, start a build and look at the beginning of the Build Results log. You'll see all the variables XCode sets. It's a lot faster than trying to guess what to google. A lot faster.