Code Coverage of C++ Header Files

Hi SQ community!

I have a question regarding code coverage analysis.

In our project, we use gcov for analyzing coverage for our C++ code and publish it to a SQ server to render results. It works pretty well so far but we are facing problems with header-only code, templates in particular. It is always shown as 0% covered although unit tests are executed that call the functions. Invoking a template function that is just declared inside the header but defined and used inside a cpp file only works fine.

Does anybody have an idea how to fix this?

I’ve spend several hours now to try out different things, e.g., playing around with compiler options, but none of them worked.

Sample .hpp file:

#include <iostream>

static auto i = 0;

inline void inlineFunction() {
	std::cout << "inlineFunction(); i = " << i++ << std::endl;

void regularFunction();

template< typename T >
void templateFunctionInHeader( T&& arg ) {
	std::cout << "templateFunctionInHeader(" << std::forward< T >( arg ) << "); i = " << i++ << std::endl;

void regularFunctionCallingTemplateFunctionInSource();

The corresponding .cpp file:

#include "SQTest.hpp"

void regularFunction() {
	std::cout << "regularFunction(); i = " << i++ << std::endl;

template< typename T >
void templateFunctionInSource( T&& arg ) {
	std::cout << "templateFunctionInSource(" << std::forward< T >( arg ) << "); i = " << i++ << std::endl;

void regularFunctionCallingTemplateFunctionInSource() {
	templateFunctionInSource( 42 );
	templateFunctionInSource( std::string{"X"} );

Coverage reports for the header:

Coverage report for the source:

Hello @fbeier,

Here SonarQube is just reading the report and displaying it. If the problem is in the report we cannot help. What you are describing looks like gcov related issue. You have a higher chance of getting an answer if you post the question in gcov related channel.


Thanks @Abbas_Sabra!

I already assumed something like that. I just hoped that other people using SQ for C++ development already faced the problem and know a solution, which is probably pretty simple.

1 Like

We found out the root cause of the issue. TL;DR: A setup issue because coverage analysis was missing for generated code.

The details in case anyone needs them:

  • We use the CxxTest framework for our unit tests that are the base of the coverage analysis.
  • The framework takes a collection of header files and generates C++ source files for the test runners that are used to compile test executables.
  • The generated test runners #includes the header-only code and links with the project libraries comprising the code from the .cpp files.
  • The gcov analysis was just executed on the library code and, thus, only contained coverage results for the corresponding .cpp sources.
  • The gcov analysis was missing for the generated test runners and, thus, lacks coverage for the header-only parts that were not used by the libraries.
  • Including the gcov analysis for the generated part fixed the issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.