Linux – gcov generates empty coverage for c

ccode-coveragegcovlcovlinux

I'm trying to collect code coverage for the project that has c++ and c code both on Ubuntu.

I use '-fprofile-arcs' and '-ftest-coverage' values as CXXFLAGS and CFLAGS; '-lgcov' as LINKFLAGS.

Common C project structure is:

c_code\
     src
     unit_tests

src contains sources of static library.

unit_tests dir contain tests written with googletest framework e. g. tests of kind

TEST_F(test_case_name, test_name) {
    some_gtest_assertions;
}

After the build googletest binary that should contain static library to test inside of it is launched.

Building and running of the project binaries results in generating of *.gcno and *.gcda files. However my C coverage results are empty (C++ is generated well).

lcov command has the folloiwng format:

lcov --capture --directory my_c_gcda_location --output-file c_coverage.info

Logs of lcov show the following for C-related gcda files:

gcov did not create any files for "my_c_gcda_location/*.gcda"`

There are also errors of kind:

*.gcda:stamp mismatch with notes file

Should I specify some additional parameters or perform some additional actions to get coverage results for C? Or what can be the reason of these issues?

Best Answer

You may get "stamp mismatch" when the .gcda files are newer than the .gcno files.

It can happen mostly for 2 reasons: 1. You might have re-build the code after running the test and before trace file generation. 2. The binary might be built in one machine and test was ran in other machine, whose time is ahead of the build machine.

For these two cases you have to just make sure the file creation time of .gcda is greater than .gcno and .c* files.

You can do that by just doing "touch *.gcda" and then run the lcov command.

Related Topic