Branches and precompiled headers in XCode

· by Steve · Read in about 2 min · (288 Words)

Here’s a quick tip for you - XCode helpfully makes using precompiled headers in your project a cinch, even easier than trusty old MSVC in fact, which is a good thing. Unfortunately, it also places the result of said precompilation step in a shared location by default, namely /Library/Caches/$(UID)/SharedPrecompiledHeaders. It actually creates folders in this location corresponding to each combination of target name and a hash of the compiler settings used. The idea here, which seems sound to begin with, is that it wants to try to re-use the results of the precompilation as many times as possible.

Again, this unfortunately becomes a problem the minute you want to use multiple branches of the same project / target, because the target names and compiler hashes are identical, but the headers themselves can be very different - and you start getting funny compiler errors when everything in the code looks perfectly fine. The problems of course go away the minute you hit ‘Clean’ or touch a header that’s included in the PCH, and I’d missed this problem until now because I just happened to be jumping between branches relatively infrequently on OS X so one of those conditions masked the problem. Then all of a sudden it bites me in the ass when I’ve seemingly made no changes.

It’s easy to fix, just hit Apple-I on the top-level project node, pick ‘All Configurations’ and ‘Build Locations’ and change the ‘Precompiled Headers Cache Path’ to something relative to the project. Presto, no more branch-hopping PCH issues. Once again the default XCode setup really doesn’t like to play with multi-branch development practices, something that can confuse otherwise experienced developers who are new to the tool. Well, it confused me briefly anyway 😕