Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

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

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
", i);
      if (i % 11 == 0)
        printf("%d is divisible by 11
", 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).
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...