Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting useful GCov results for header-only libraries

Tags:

c++

gcc

gcov

For my header-only C++ library (lots of templates etc) I use GCov to check test coverage. However, it reports 100% coverage for all headers because the unused functions aren't generated by the compiler in the first place. Manually spotting uncovered functions is easy but defeats the purpose of continuous integration…

How does one solve this automatically? Should I just use "lines hit / LOC" as my coverage metric and just never reach 100% again?

like image 653
pascal Avatar asked Mar 12 '12 12:03

pascal


People also ask

What is difference between gcov and LCOV?

Lcov is a graphical front-end for gcov. It collects gcov data for multiple source files and creates HTML pages containing the source code annotated with coverage information. It also adds overview pages for easy navigation within the file structure. Lcov supports statement, function, and branch coverage measurement.

What is gcov used for?

gcov is a test coverage program. Use it in concert with GCC to analyze your programs to help create more efficient, faster running code and to discover untested parts of your program. You can use gcov as a profiling tool to help discover where your optimization efforts will best affect your code.


1 Answers

Apart from the usual flags to GCC controlling inlining;

--coverage -fno-inline -fno-inline-small-functions -fno-default-inline 

You can instantiate your template classes at the top of your unit test files;

template class std::map<std::string, std::string>; 

This will generate code for every method in that template class making the coverage tools work perfectly.

Also, make sure that you initialise your *.gcno files (so for lcov)

lcov -c -i -b ${ROOT} -d . -o Coverage.baseline <run your tests here> lcov -c -d . -b ${ROOT} -o Coverage.out lcov -a Coverage.baseline -a Coverage.out -o Coverage.combined genhtml Coverage.combined -o HTML 
like image 92
Marcus O Platts Avatar answered Sep 25 '22 10:09

Marcus O Platts