Question

I am trying to use gcov on Linux(Ubuntu) to see frequency of execution for each line of source.

I have added following flags to my gcc compiler and linker flags,

CCFLAGS =  -fprofile-arcs -ftest-coverage
LDFLAGS = -fprofile-arcs -lgcov

but after compiling and running the program, i see no *.gcda file created. As a result of which when i run

gcov  --object-directory <path to the *.gcno/*.gcda files> myfile.cpp 

Shows error:

myfile.gcda:cannot open data file, assuming not executed
File '../../../../../code/myfile.cpp'
Lines executed:0.00% of 2625

Am i missing something. How to fix this?

Was it helpful?

Solution

You can use __gcov_flush() method inside your code. You will need to invoke this from registered signal handler.

See:

https://www.osadl.org/fileadmin/dam/interface/docbook/howtos/coverage.pdf

Using this, you can keep your service running and issue "kill" whenever you need to dump coverage data.

Hope that helps....

OTHER TIPS

make sure the gcov and gcc are of the same version :), this where many of the people fail to check.

$gcc --version
gcc (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ find  /usr/lib/gcc -name libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/32/libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/32/libgcov.a
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/libgcov.a

so the gcc version you have loaded should be atleast in the available list of libgcov.a

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top