Search code examples
c++gccg++gcovlcov

gcov function is not executed but lines are


I have been using gcov to check code coverage. I now get a weird result, some functions are listed as not executed but the lines in the function is listed as executed.

lcov output:

  13           0 :     f<double>& operator*=(f<double>& lhs, const double& rhs)
  14             :     {
  15           9 :         // Some code...
  16           0 :         return lhs;
  17             :     }

gcov output:

#####:   13:    f<double>& operator*=(f<double>& lhs, const double& rhs)
    -:   14:    {
    9:   15:        // Some code...
#####:   16:        return lhs;
    -:   17:    }

How can the function be listed as NOT executed when the line(s) are? Is this possibly an artifact of GCC's optimization?

EDIT 1:

G++/gcov version: 4.9.2

lcov version: 1.11

EDIT 2:

Comping with -O2 yields the weird behavior. However, compiling with -O1 changes the #### to 9


Solution

  • You should run gcov on unoptimised code. This will give you output, perhaps interpreted by lcov, which will assure you that all lines have run.

    You can then compile with optimisations on for a release version.