Search code examples
cgcccode-coveragegcov

Why two compiler flags needs to be passed for code coverage in GCC


I understand two compiler flags: -ftest-coverage -fprofile-arcs needs to be passed for getting code coverage in GCC. My question is, what is the reasoning for having 2 compiler flags for getting coverage. Also, what can we get if we use them independently?.

I tried compiling a c program with only -fprofile-arcs flag. I didnt notice any differences. Was able to generate .gcno .gcda and gcov files


Solution

  • If you check the documentation for -fprofile-arcs you will see that the data it generates can be used for two different things depending on other options: -ftest-coverage and -fbranch-probabilities.

    So the -fprofile-arcs is to generate code that does instrumentation and saves data. Then you use either -ftest-coverage or -fbranch-probabilities to specialize the data depending on what analysis you want to perform.

    It doesn't say anywhere, but from your experience it seems that GCC defaults to -ftest-coverage if none of the specialization flags are provided.