LinuxCommandLibrary

gcov

Analyze code coverage

TLDR

Generate a coverage report named file.cpp.gcov

$ gcov [path/to/file.cpp]
copy

Write individual execution counts for every basic block
$ gcov [[-a|--all-blocks]] [path/to/file.cpp]
copy

Write branch frequencies to the output file and print summary information to stdout as a percentage
$ gcov [[-b|--branch-probabilities]] [path/to/file.cpp]
copy

Write branch frequencies as the number of branches taken, rather than the percentage
$ gcov [[-c|--branch-counts]] [path/to/file.cpp]
copy

Do not create a gcov output file
$ gcov [[-n|--no-output]] [path/to/file.cpp]
copy

Write file level as well as function level summaries
$ gcov [[-f|--function-summaries]] [path/to/file.cpp]
copy

SYNOPSIS

gcov [OPTION...] sourcefile

PARAMETERS

-a, --all-blocks
    Include all blocks in summary, even unexecuted

-b, --branch-probabilities
    Show branch probabilities and taken branches

-c, --demangled-names
    Demangle C++ function names

-f, --function-summaries
    Per-function summaries only

-h, --human-readable
    Human-readable numbers (e.g., 1.2k)

-i, --intermediate-format
    Emit gcov intermediate format

-j, --json-format
    Output in JSON format

-k, --use-colors
    Colorize output

-l, --long-file-names
    Long .gcov names for multiple inputs

-m, --show-mismatches
    Report line mismatches

-n, --no-output
    No .gcov output files

-o DIR|FILE
    Data file directory or path

-p, --preserve-paths
    Preserve path components

-r, --relative-only
    Process relative paths only

-s DIR
    Source path prefix

-u, --unconditional-branches
    Include unconditional branches

-v, --version
    Print version info

--help
    Display help

DESCRIPTION

Gcov is a command-line tool in the GNU Compiler Collection (GCC) suite for test coverage analysis. It processes profile data from instrumented programs to report which lines, functions, branches, and blocks of source code were executed during testing.

To use gcov, first compile source files with gcc -fprofile-arcs -ftest-coverage. This generates .gcno files (static trace data) alongside object files. Running the executable produces .gcda files (dynamic arc counts). Then, run gcov source.c to create an annotated source.c.gcov file showing execution counts per line, with summaries of coverage percentages.

Unexecuted lines appear with ###### or ##### markers; executed lines show hit counts. It supports branch probabilities, function summaries, and output in human-readable, JSON, or intermediate formats. Ideal for regression testing, identifying dead code, and improving test suites in C/C++ projects.

Gcov integrates with tools like lcov for HTML reports. It focuses on line/branch coverage, not statement-level like some profilers.

CAVEATS

Gcov requires matching .gcno/.gcda files from GCC instrumentation. Optimized code (-O) distorts results; use -O0. Multi-threaded apps need care for .gcda writes. Ignores assembler/debugger data.

BASIC USAGE

gcc -fprofile-arcs -ftest-coverage foo.c -o foo
./foo
gcov foo.c
Inspect foo.c.gcov for counts.

HISTORY

Introduced in GCC 1.x (early 1990s) as part of profiling tools. Enhanced in GCC 4+ with branch coverage, GCC 9+ added JSON/color outputs. Tied to GCC evolution for C/C++/Fortran coverage.

SEE ALSO

gcc(1), gcov-dump(1), gcov-tool(1)

Copied to clipboard