LinuxCommandLibrary

lcov

Generate code coverage reports

SYNOPSIS

lcov [OPTION]...

PARAMETERS

-a, --add-tracefile FILE
    Add tracefile FILE to list for combining coverage data

-b, --base-directory DIR
    Set base directory for relative source paths

-c, --capture
    Capture coverage data from gcov directory tree

-d, --directory DIR
    Target directory for data capture or base tree

-e, --extract PATTERN
    Extract data matching pattern (shell glob)

-f, --filter PATTERN
    Filter data to match pattern only

-h, --help
    Display help and exit

-H, --html DIRECTORY
    Generate HTML report in DIRECTORY

-i, --initial
    Use initial (zeroed) coverage counters

-l, --list
    List contents of lcov tracefiles

-o, --output-file FILE
    Write output to FILE (default: stdout)

-p, --prefix DIR
    Strip prefix DIR from source file paths

-q, --quiet
    Suppress progress messages

-r, --remove PATTERN
    Remove data matching pattern

--rc KEY=VALUE
    Set runtime configuration option

-s, --summary [FILE]
    Output simple coverage summary

-z, --zerocounters
    Reset all gcov counters to zero

--version
    Print version information

DESCRIPTION

lcov is a powerful command-line tool for code coverage analysis, serving as a front-end to GCC's gcov utility. It processes coverage tracefiles (.gcda and .gcno files) generated by compiling C/C++ code with -fprofile-arcs -ftest-coverage flags and executing instrumented binaries. lcov aggregates this data into human-readable formats, including text summaries and interactive HTML reports.

Core functionalities include capturing raw coverage from directories, combining multiple tracefiles, filtering by file patterns, removing unwanted data, and computing metrics like statement, branch, and function coverage percentages. It excels in CI/CD pipelines for assessing test completeness.

Workflow example: Compile with coverage flags, run tests to generate .gcda files, then use lcov --capture --directory . --output-file coverage.info. Pipe to genhtml (bundled with lcov) for HTML: genhtml coverage.info --output-directory report. Supports large projects via exclusion lists and relative path handling.

Ideal for developers ensuring high test coverage in open-source and enterprise software.

CAVEATS

Requires GCC-generated .gcda/.gcno files; not compatible with Clang/LLVM without workarounds. Large projects may produce huge reports without filtering. No support for line execution counts exceeding 32-bit limits in old versions.

TYPICAL WORKFLOW

1. gcc -fprofile-arcs -ftest-coverage -o prog src.c
2. ./prog (run tests)
3. lcov --capture --directory . --output-file cov.info
4. genhtml cov.info --output-directory ./html
Open ./html/index.html for report.

OUTPUT FORMAT

Produces LCOV info files (.info) with human-readable coverage data, parsable by other tools. Metrics: lines executed, branches, functions.

HISTORY

Developed by Christian Cerveira in 2002 for the Linux Test Project (LTP). Hosted on SourceForge; version 1.15 released in 2021 with improved HTML output and JSON support. Widely used in open-source C/C++ projects.

SEE ALSO

gcov(1), genhtml(1), gcovr(1)

Copied to clipboard