Fundamentals 8 min read

Introduction to C/C++ Code Coverage Tools and Practices (BullseyeCoverage, gcov/lcov)

This article explains the concepts of code coverage, describes different coverage metrics such as statement, branch, and function coverage, introduces BullseyeCoverage and gcov/lcov tools, and provides detailed step‑by‑step procedures for measuring coverage in both executable binaries and dynamic libraries for C/C++ projects.

Baidu Intelligent Testing
Baidu Intelligent Testing
Baidu Intelligent Testing
Introduction to C/C++ Code Coverage Tools and Practices (BullseyeCoverage, gcov/lcov)

Code coverage measures how much of a program's source code is executed during testing, with common metrics including statement (line) coverage, branch (decision) coverage, and function coverage.

The article first defines these metrics and then introduces two major tools: BullseyeCoverage, a commercial C/C++ coverage solution that reports branch coverage only, and the free gcov/lcov suite, which can report statement, branch, and function coverage.

BullseyeCoverage usage includes creating a build directory, ensuring compatibility with the C++ standard (e.g., using -std=c++11 for older versions), compiling the code with the tool, running the program with a specified test.cov file, and finally viewing coverage results via covclass , covdir , and covfn .

gcov/lcov workflow covers compiling with -coverage -lgcov -fprofile-arcs , generating .gcno files, executing the program, sending a SIGUSR1 (or kill -10 ) to produce .gcda files, and then using lcov commands such as:

lcov -c -o test_cov_full.info -d . --rc lcov_branch_coverage=1

to collect raw data, followed by extraction of relevant directories:

lcov -e test_cov_full.info $PWD -o test_cov_strategy.info --rc lcov_branch_coverage=1

and finally generating an HTML report:

genhtml test_cov_strategy.info -o test_cov_strategy --rc lcov_branch_coverage=1

For projects that include dynamic libraries, the article explains the need to add a flush function in each shared library and to handle a custom signal in the main program so that coverage data from the libraries is written out. Sample export.c files and Makefile modifications are shown to integrate the flush mechanism.

The guide concludes with instructions for building the framework and strategy code, running automated test cases, and verifying that .gcda files are produced for both the main executable and the shared libraries, enabling comprehensive coverage analysis.

code coveragetestingcgcovlcovBullseyeCoverage
Baidu Intelligent Testing
Written by

Baidu Intelligent Testing

Welcome to follow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.