13

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.

4

3 回答 3

15

我解决了这个问题。

我错过了以下链接器标志

-Wall -fprofile-arcs -ftest-coverage

于 2013-10-11T13:05:53.120 回答
6

其他链接器标志-fprofile-arcs为我解决了这个问题。

Build Settings > Other Linker Flags > -fprofile-arcs
于 2016-09-19T17:57:26.077 回答
4

上面的答案在 Xcode 6.3.1 的 OSX Yosemite (10.10.3) 上对我不起作用。似乎 Apple 移动了这些库。我能够让它与以下编译选项一起工作:

-lclang_rt.profile_osx 
-L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/6.1.0/lib/darwin
于 2015-05-18T22:03:48.783 回答