Using Gcov and Lcov

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

USING GCOV AND LCOV TO

GENERATE BEAUTIFUL C++ CODE


COVERAGE STATISTICS
December 26, 2014subho#openSource, c++, code-coverage, cppagent, gcov, lcov
We all know, testing is an important part of a project. But how efficient are
your tests? How much of your codes have you tested? Here comes the role of
code coverage tools. I recently got to work on a C++ project, and a code
coverage tool (gcov and lcov) .

In this post i have taken a sample C++ program and will be generating the
code coverage stats for the same using gcov and lcov. Here is my sample
C++ program link. Its pretty simple menu driven program that does simple
mathematical operations like addition, subtraction, multiplication and
division depending on users choice.
In this demo i am not writing actual test-cases for the code but you can see
the changes in the coverage graphs depending upon your choice.

To start with we need to install gcov. Gcov comes with gcc compiler. So if you
have gcc installed then gcov will work for you. Next you need to have lcov. I
am working on Fedora 21, so for me its a yum install.

1 $yum install lcov


Next lets start with compiling our code. Here my source file name is
menu.cpp

1 $g++ -o menu.out --coverage menu.cpp


The coverage option here is used to compile and link code needed for
coverage analysis. You will find a menu.gcno file in the folder. Next we need
to export two variables namely GCOV_PREFIX
and GCOV_PREFIX_STRIP. Set GCOV_PREFIX to the folder you want the
output files to be in.
$ls

menu.cppmenu.outmenu.gcnodata //youcanseethenewfilemenu.gcno
For me , the project is in /home/subho/work/lab/zzz/ and inside this i
have created a folder named data where i want the data files or .gcda file
to be generated. so i set my GCOV_PREFIX to
/home/subho/work/lab/zzz/data and the GCOV_PREFIX_STRIP equal to
the the number of forward slashes or / in the path.
1 $export GCOV_PREFIX="/home/subho/work/lab/zzz/data"
1 $export GCOV_PREFIX_STRIP=6
now lets simply run the code.

1 $./menu.out
1
2 MENU
3 1: Add
4 2: Subtract
5 3: Multiply
4: Divide
6 5: Exit
7 Enter your choice :2
8 Enter two numbers: 3 4
9 Difference -1
10 MENU
1: Add
11 2: Subtract
12 3: Multiply
13 4: Divide
14 5: Exit
Enter your choice :5
15
16
Now we can see a menu.gcda file in data folder. Copy the .gcno file
generated earlier to the data folder.
1 $cd data
2
3 $ls
4
5 menu.gcda
6
7 $cp ../menu.gcno .
8
$ls
9
10
menu.gcda menu.gcno
11
Now that we have all the necessary files lets use lcov to read the coverage
output file generated by gcov.

1 $lcov -t "result" -o ex_test.info -c -d .


Here ex_test.info is my output file.
-t sets a test name

-o to specify the output file

-c to capture the coverage data

-d to specify the directory where the data files needs to be searched


Now we will generate out html output for the statistics.

1 $genhtml -o res ex_test.info


-o To specify the output folder name.

Now on doing ls, you can see a folder named res.


$ls

ex_test.infomenu.gcdamenu.gcnores
Now its time to enjoy the fruits of your labor . Go into the res folder and

start a server or you can simply open the index.html file in your web-
browser.
1 $cd res
2 $python -m "SimpleHTTPServer" //to start a web-server or
3 $firefox index.html //to open the index.html directly using firefox brows
Now we can click on the links to check the code coverage stats.
The Red lines are the ones not executed or uncovered region. The blue lines
are the ones covered. Also you can look at the Line data section for the
number of times the lines have been executed.
You can look at these files in GitHub.

You might also like