No code coverage with Mac OS X Lion and XCode 4 / llvm-g++-4.2

感情迁移 提交于 2019-11-28 08:27:42

问题


Other people have reported not being able to generate code coverage with XCode 4, but I find not only can I not do it from within XCode 4, I can't do it even with a simple toy program from the command line. I followed the examples given here and here, which led me to create this cov.c file:

#include <stdio.h>

int main (void) {
  int i;
  for (i = 1; i < 10; i++) {
      if (i % 3 == 0)
        printf("%d is divisible by 3\n", i);
      if (i % 11 == 0)
        printf("%d is divisible by 11\n", i);
  }
  return 0;
}

I then used the following commands in an attempt to generate code coverage:

g++ -c -g -O0 --coverage -o $PWD/obj/cov.o $PWD/cov.c
g++ -g -O0 --coverage -o $PWD/bin/cov $PWD/obj/*.o
$PWD/bin/cov

Alas, no cov.gcno file exists in the obj directory. In fact, the only files I have after this are: cov.c obj/cov.o bin/cov

Furthermore, if I type nm bin/cov, I get the following:

0000000100001048 S _NXArgc
0000000100001050 S _NXArgv
0000000100001060 S ___progname
0000000100000000 A __mh_execute_header
0000000100001058 S _environ
                 U _exit
0000000100000e40 T _main
                 U _printf
0000000100001000 s _pvars
                 U dyld_stub_binder
0000000100000e00 T start

This suggests that libgcov.a was never linked in. If I replace

g++ -g -O0 --coverage -o $PWD/bin/cov $PWD/obj/*.o

with:

g++ -g -O0 --coverage -o $PWD/bin/cov -lgcov $PWD/obj/*.o

I get the exact same results.

More information:

  • g++ --version yields: "i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)"
  • I've also tried using gcc (which is llvm-gcc).

回答1:


I was able to figure out an answer to this question using help from this answer. Basically, I changed my coverage commands to use clang instead of g++ (because the example file was pure C, I went with clang instead of clang++, which I've verified works just fine with C++ files). From there, I was able to use lcov to generate output similar to what I'm used to seeing from Java/cobertura.




回答2:


On Lion g++ is an alias for llvm-g++, as you have discovered. To invoke "real" gcc, use gcc-4.2 or g++-4.2:

g++-4.2 -g -O0 --coverage -o $PWD/bin/cov $PWD/obj/*.o


来源:https://stackoverflow.com/questions/8622293/no-code-coverage-with-mac-os-x-lion-and-xcode-4-llvm-g-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!