I'm trying to get gcov results on a file that's brought in via #include
. If I compile with it as a separate object file it works fine. For example with these files:
lib.h
int addFive(int num);
lib.c
#include "lib.h"
int addFive(int num)
{
return num + 5;
}
testlib.cpp
#include "lib.h"
int main()
{
return addFive(2);
}
If I then compile like so:
g++ -c -fprofile-arcs -ftest-coverage lib.c -o lib.o
g++ -c testlib.cpp -o testlib.o
g++ lib.o testlib.o -lgcov -o testlib
then I will get a gcda file as expected:
[~]$ strings testlib | grep gcda
/home/user/lib.gcda
[~]$
But if I attempt this when including the .c file like below
test.cpp
#include "lib.c"
int main()
{
return addFive(2);
}
And compile that
g++ test.cpp -lgcov -o test
Then I don't get the gcda as expected:
[~]$ strings test | grep gcda
[~]$
Is there a way to get the gcda to show up when including a C file? The same applies to including a .cpp.
If I compile test with --coverage
instead of -lgcov
it does generate a test.gcda
. I thought at first this was not what I wanted since I'm looking for coverage in lib.c
But if I run both testlib
and test
and then use gcov on all the files together, it combines the coverage data properly:
gcov lib.gcda lib.gcno test.gcda test.gcno
And this yields lib.c.gcov
with code coverage from both test runs.