Skip to content

GCC Code Coverage

Published On:
Nov 29, 2017
Last Updated:
Nov 29, 2017

GCC has built in functionality to generate code coverage data for libraries and executables you build with it. A number of other GNU tools are provided which can then read and display the generated coverage data

Coverage monitoring code can be inserted into your executable by providing gcc with the -coverage flag when compiling (which is shorthand for providing the the flags -fprofile-arcs -ftest-coverage):

Terminal window
$ gcc example.c -o example -coverage;

When running the compiled executable, .gcda files should be created for each source file.

lcov can be used to view the coverage data. lcov can be installed on Debian-like systems such as Ubuntu with:

Terminal window
$ sudo apt install lcov

You can then “capture” the data with lcov using:

Terminal window
$ lcov --capture --directory <path> --output-file coverage.info

And then generate HTML files with:

Terminal window
$ genhtml coverage.info --output-directory out

These visual HTML files are put into the out/ directory and can be viewed in your browser.

Troubleshooting

undefined reference to __gcov_merge_add

This is usually because the gcov library has not been linked with your library/executable. Usually this will happen automatically if you compile and link with the same gcc command. If compiling and linking separately (or using something like CMake), you may have to provide the linker flags -coverage and -lgcov.