code coverage for c program on linux

April 6, 2011 at 6:25 PM | Posted in Uncategorized | 1 Comment

The setup used here is FC12 linux machine.

Here we will use  “gcov” tool for code coverage.  In addition with gcov, we will use “lcov” and “genhtml” for  output in HTML formatt. To know more about these tools and options you can refer their man pages.

1) First install these packages: gcov, lcov and genhtml. You can install from sources or yum.

2) To test the code coverage for a c source program, the program must be compiled with  “-fprofile-arcs” and -”ftest-coverage” flags.

$ gcc -fprofile-arcs  -ftest-coverage test.c – o test

3) Then run the program.

$  ./test

It will create 2  additional file with extensions gcno and gcda. In this example it should be test.c.gcno and test.gcda

4) Then use lcov tool to generate html output file.

$ lcov –directory . -c -o test.info . Here test.info is output file.

5) Using genhtml, generate hrtml file from lcov data

$ genhtml test.info

It will generate few html files. You can view the html files using broweser or htmlview command

$ htmlview index.html

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.