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).
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.
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