This tutorial is just a small follow-up on one of my earlier posts about how to set up code coverage in Xcode.
With the release of Xcode 4.2 code coverage is finally supported using Clang / LLVM. Opposed to what I described in the earlier post you no longer need to force the use of GCC to get code coverage metrics in your unit tests. Since Apple has also decided to drop support for GCC you are more or less forced to switch to Clang / LLVM anyway.
In Xcode 4.2 it’s fairly simple to set up code coverage. If you have defined a custom build setting or build rule that enforces GCC 4.2 you will need to remove those first.
Next you’ll need to enable the two build settings “Generate Test Coverage Files” and “Instrument Program Flow” as shown below:
Under the Linker settings you may also notice that the “-lgcov” linker flag has been removed. This is because code coverage on Clang / LLVM is now supported by the “profile_rt” library. Unfortunately it’s a bit tricky to find this library because Apple for whatever reason decided not to include it in the default library path. Instead you’ll have to manually navigate to /Developer/usr/lib/ to link against it:
That’s all there is to it.
Just run your unit tests and view the code coverage data as usual.
The updated Xcode project can be downloaded from GitHub.