The gcovr is a command-line tool used to generate readable coverage reports from GCC coverage data. It simplifies analysis of test coverage by summarizing execution results produced during compilation and runtime. This tutorial explains how to install gcovr on Ubuntu 26.04.
Prepare environment
Before starting, ensure that the GCC compiler is installed.
sudo apt install -y gcc
Install gcovr
Get the latest release version number from GitHub:
GCOVR_VERSION=$(curl -s "https://api.github.com/repos/gcovr/gcovr/releases/latest" | grep -Po '"tag_name": "\K[0-9.]+')
Download the appropriate binary:
sudo curl -sSLo /usr/local/bin/gcovr https://github.com/gcovr/gcovr/releases/latest/download/gcovr-$GCOVR_VERSION-linux-x86_64
Grant execution permission:
sudo chmod a+x /usr/local/bin/gcovr
Confirm installation by checking gcovr version:
gcovr --version
Testing gcovr
A small C program is used to demonstrate coverage generation. Create a source file:
nano main.c
Add the following code:
int is_positive(int x) {
if (x > 0) {
return 1;
}
return 0;
}
int main(void) {
is_positive(1);
return 0;
}
Before compiling the program, coverage instrumentation needs to be enabled so that GCC can generate the required metadata for later analysis. This is done by adding the --coverage option along with debugging symbols and reduced optimization to ensure accurate results.
gcc --coverage -g -O0 main.c -o test
After compilation, GCC produces an additional file such as test-main.gcno, which stores coverage mapping information used during execution and report generation.
After compilation with coverage support, the program needs to be executed so that runtime coverage data can be collected. During execution, the instrumented binary records which parts of the code are actually reached.
./test
Once the program finishes running, GCC generates a corresponding runtime data file such as test-main.gcda. This file is used together with the previously created .gcno file to compute the final coverage report.
After the program execution, the next step is to analyze coverage data files with gcovr tool and generate a human-readable report. The tool scans the current directory, reads the .gcno and .gcda files, and summarizes which lines of code were executed.
gcovr
The output includes a structured overview showing total lines, executed lines, and coverage percentage for each file.
(INFO) Reading coverage data...
(INFO) Writing coverage report...
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Lines Exec Cover Missing
------------------------------------------------------------------------------
main.c 7 6 85% 6
------------------------------------------------------------------------------
TOTAL 7 6 85%
------------------------------------------------------------------------------
Uninstall gcovr
To uninstall gcovr from the system, the binary can be removed as follows:
sudo rm -rf /usr/local/bin/gcovr
Leave a Comment
Cancel reply