Clang Code Coverage – Mac OS X – Linker Error


Question or issue on macOS:

I could successfully get code coverage information on our C++ code base on Linux using the GCC features of GCOV and the LCOV tool.

But I am having trouble in Mac OS X.

As Apple does not have the classic GCC compiler anymore, and we fear that the LLVM-GCC compiler would one day disappear too (LLVM-GCC is not even available as an option in Xcode 5.0) – we have decided to use Clang to compile our code.

While using the Clang compiler I am passing in these flags –>
-g -fprofile-arcs -ftest-coverage to generate the Code Coverage information.

I can see the .gcno files getting generated along with the object files.

When it comes to linking – “-lgcov” linker flag which works with GCC is not supported.

The 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 /usr/lib/ to link against it:

And as specified am linking against libprofile_rt.a library.

But i have linker issues.

But i keep getting these linker errors

Undefined symbols for architecture x86_64:
  "_llvm_gcov_init", referenced from:
      ___llvm_gcov_init in Iso9660Reader.o
      ___llvm_gcov_init in AutoExtractCreator.o
      ___llvm_gcov_init in TempFilePath.o
      ___llvm_gcov_init in TempPath.o
      ___llvm_gcov_init in ReadDirectory.o
      ___llvm_gcov_init in OpenDirectory.o
      ___llvm_gcov_init in SpltPath.o
ld: symbol(s) not found for architecture x86_64 

I also tried linking against the dynamic library – libprofile_rt.dylib found in
/usr/lib folder – But i still get the same issue.

This is Clang Version running on Mountain Lion.

clang --version
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

I also have Xcode 5.0 and Developer Tools installed.

How to solve this problem?

Solution no. 1:

I solved this.

I was missing the following Linker Flags

-Wall -fprofile-arcs -ftest-coverage

Solution no. 2:

Other Linker Flag -fprofile-arcs fixes the issue for me.

Build Settings > Other Linker Flags > -fprofile-arcs

Solution no. 3:

The above answer did not work for me on OSX Yosemite (10.10.3) with Xcode 6.3.1. It seems that Apple moved these libraries around. I was able to get it to work with the following compile options:


Hope this helps!