gcc Code coverage: gcov Generate Output


To generate the coverage information the compiled program should be executed. When creating code coverage for a test suite this execution step will normally be performed by the test suite so that the coverage shows what parts of the program the tests executes and which they do not.

$ a.out

Executing the program will cause a .gcda file to be generated in the same directory as the object file.

Subsequently you can call gcov with the program's source file name as an argument to produce a listing of the code with frequency of execution for each line.

$ gcov hello.c
File 'hello.c'
Lines executed:90.00% of 10
Creating 'hello.c.gcov'

The result is contained in a .gcov file. Here is a sample:

         -:    0:Source:hello.c
         -:    0:Graph:hello.gcno
         -:    0:Data:hello.gcda
         -:    0:Runs:1
         -:    0:Programs:1
         -:    1:#include <stdio.h>
         -:    2:
         -:    3:int main (void)
         1:    4:{
         1:    5:  int i;
         -:    6:
         1:    7:  i = 0;
         -:    8:
         -:    9:
         1:   10:  if (i != 0)
     #####:   11:    printf ("Goodbye!\n");
         -:   12:  else
         1:   13:    printf ("Hello\n");
         1:   14:  return 0;
         -:   15:}

Here you can see the line numbers and source and the number of times each line executed. If a line did not execute it is marked with #####.

The execution counts are cumulative. If the example program were executed again without removing the .gcda file, the count for the number of times each line in the source was executed would be added to the results of the previous run.