Gcov is a tool you can use in conjunction with GCC to test code coverage in your programs. This extension visualizes the output of gcov in Visual Studio Code. It allows you to see which lines of the program have been executed and how often.
- Compile your code with
gcc
/g++
(version >= 9) with--coverage
and without optimizations. - Run your program or tests.
- Open a source file in vscode and use
ctrl+shift+P
to run theGcov Viewer: Show
command.
During compilation, gcc
will generate a .gcno
file next to every .o
file. When you run the program, it will create .gcda
files in the same location. Those contain information about how often functions and lines have been executed.
This vscode extension uses the gcov
program to parse these additional files and displays the information on top of your source files. For this to work, it has to find the generated .gcda
for a given project. Those are usually in your build directory. If the extension does not find them on its own, you have to edit the gcovViewer.buildDirectories
setting of your workspace folder. The most convenient way to do this is to use the Gcov Viewer: Select Build Directory
command.
- Try passing
-fprofile-abs-path
to gcc. This helps the extension to match source files with their corresponding coverage data.
- Sometimes lines are counted more than once when they do more than one thing. That might be confusing in some cases. Usually it is quite easy to get the correct number by looking at neighboring lines.
- When the
Highlight Missed Lines
setting is enabled, some missed lines might not be marked as such.
- Only works with version 9 or higher of
gcc
,g++
andgcov
. If you have multiple versions ofgcov
, you might have to change theGcov Binary
setting.