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
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:
You can then “capture” the data with
And then generate HTML files with:
These visual HTML files are put into the
out/ directory and can be viewed in your browser.
undefined reference to
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
This work is licensed under a Creative Commons Attribution 4.0 International License .