I am trying to use gcov.开发者_如何转开发 I have this simple file a.c:
int main() {
return 0;
}
So I do
gcc -fprofile-arcs -ftest-coverage a.c -o a
./a
gcov a.c
and I get
a.gcno:cannot open graph file
Am I doing something wrong? I'm under Mac OS X Lion.
By default on Lion, "gcc" is not gcc. It's LLVM. And it doesn't support generating test coverage data.
If you run gcc-4.2 -fprofile-arcs -ftest-coverage a.c -o a
instead that will use a real gcc, and it'll probably work.
Are you sure you are running the command from the same directory as the source file? You must be in the same directory, unless you specify the -o flag. Try:
gcov -o a.c
Try using clang
instead of gcc
. I had the same problem, and using clang
fixed it for me.
Often this happens when the version of gcov
you're running doesn't match the version of GCC used to compile the application. On some systems, package managers have odd practices in how they link GCC and gcov
. For example on many systems gfortran
is the same as gfortran-5
but gcov
might be something old and crusty.
I used the following on Mac 10.8.4
:
- Installed via xcode the command line tools:
wrote this example code from the gcc website:
#include <stdio.h> main() { int i, total; total = 0; for (i = 0; i < 10; i++) total += i; if (total != 45) printf ("Failure\n"); else printf ("Success\n"); }
Compiling using the real GCC
gcc-mp-4.7 -fprofile-arcs -ftest-coverage tmp.c
And using GCC's gcov:
gcov-mp-4.7 -b tmp.c
will give you this output:File 'tmp.c' Lines executed:87.50% of 8 Branches executed:100.00% of 4 Taken at least once:75.00% of 4 Calls executed:50.00% of 2 Creating 'tmp.c.gcov'
精彩评论