How do we know we have tested out code well?
One measure is that we have executed each code statement at least once.
Code Coverage tools, like gcov, help us know which lines of the code have been executed (and
how many times)
Quality metrics and customer agreements often demand close to 100% code coverage reports
Tip
To Make the code coverage you will have to install ' GCC ' compiler
- suppose we have a main.c file and we want to see the code coverge of it
gcc -fprofile-arcs -ftest-coverage main.c
#Replace main.c with your C file./a.out
gcov main.c
#It will show the result of the code Coverage- Note that file (main.c.gcov) has been created
- If we open it It will show all the details about the coverage

- a number : indicates how many times the line got excuted
- '-' : indicates that the line is not an executable statement
- '#####' : indicates that the line is not excuted yet
- Do not use optimization options while using code coverage. That changes source code line number to machine code mapping adversely
- Running the executable just updates “.gcda” files. The “.c.gcov” has to be generated
manually for each source code file by doing “gcov <file.c>”.
This command correlates the structural information in “.gcno” file with data information in “.gcda” file, to generate textual report in “.c.gcov” file
1- Suppose we have two files main.c and test_branches.c
Compile Both Files with Coverage Flags
2- Compile your test_branches.c and main.c files with coverage flags. Make sure to link them together. ahmed is the name of the executable file!
gcc -fprofile-arcs -ftest-coverage -o ahmed main.c test_branches.c 3- Run the generated executable to produce the coverage data files (.gcda and .gcno).
.\ahmed.exe4- Run the gcov of the file :
gcov -b test_branches.c5- You will see the output the the following :
File 'test_branches.c'
Lines executed:35.71% of 28
Branches executed:100.00% of 14
Taken at least once:35.71% of 14
Calls executed:20.00% of 10
Creating 'test_branches.c.gcov'
- Line executed: the lines that is already excuted in the code
- Branches executed : Your code has 14 decision points where the program can branch out into different execution paths
Warning
Using lcov on Windows can be a bit challenging because it's primarily designed for Unix-like systems. However, you can set it up using a combination of Cygwin or MSYS2, which provide Unix-like environments on Windows.
-
Install MSYS2:
- Download the MSYS2 installer from the MSYS2 website.
- Run the installer and follow the installation instructions.
-
Update MSYS2 Packages:
- Open the MSYS2 shell (
MSYS2 MinGW 64-bitorMSYS2 MinGW 32-bitdepending on your system architecture). - Update the package database and core system packages by running:
pacman -Syu
- Restart the MSYS2 shell after the update.
- Open the MSYS2 shell (
-
Install Required Packages:
- Install GCC, gcov, lcov, and other essential tools by running:
pacman -S base-devel mingw-w64-x86_64-toolchain lcov
- Install GCC, gcov, lcov, and other essential tools by running:
-
Compile Your Code with Coverage Flags:
- Open the MSYS2 MinGW shell (
MSYS2 MinGW 64-bitorMSYS2 MinGW 32-bit). - Navigate to your project directory.
- Compile your code with
-fprofile-arcsand-ftest-coverageflags:gcc -fprofile-arcs -ftest-coverage -o calc calc.c
- Open the MSYS2 MinGW shell (
-
Run Your Executable:
- Run the compiled executable to generate coverage data files (.gcda and .gcno):
./calc
- Run the compiled executable to generate coverage data files (.gcda and .gcno):
-
Generate Coverage Data with lcov:
- Capture the coverage data:
lcov --capture --directory . --output-file calc.info - This will create a
calc.infofile containing the coverage data.
- Capture the coverage data:
-
Generate HTML Report with genhtml:
- Generate an HTML report from the
calc.infofile:genhtml calc.info --output-directory out
- Generate an HTML report from the
-
View the HTML Report:
- Open the
index.htmlfile in theoutdirectory using any web browser to view the coverage report.
- Open the
By following these steps, you can successfully generate GUI reports for code coverage using lcov on Windows.
If you run this
gcov -b test_branches.cand got this
test_branches.gcno:cannot open notes file
test_branches.gcda:cannot open data file, assuming not executed
No executable linesthis could help: 🫡
gcov -b -o . main-test_branches.gcdaFile 'test_branches.c'
Lines executed:35.71% of 28
Branches executed:100.00% of 14
Taken at least once:35.71% of 14
Calls executed:20.00% of 10
Creating 'test_branches.c.gcov'