Nicolás Brailovsky


A modern blog

A Makefile for code coverage report with C++

author Posted by: nico on date Aug 30th, 2011 | filed Filed under: Makefiles

So far you should know how to use makefiles and you should have a nice testable project. Then you have everything ready to get a coverage report. Yeah, using makefiles, you guessed!

This time we’ll depend on two tools, gcov and gtest. These are in Ubuntu’s repositories, so you should have no problem getting them. I won’t even bother to explain this makefile (not because it’s obvious but because I don’t really remember how it works. I wrote this over a year ago).

  1. .PHONY: clean coverage_report
  2. coverage_report:
  3.         # Reset code coverage counters and clean up previous reports
  4.         rm -rf coverage_report
  5.         lcov –zerocounters –directory .
  6.         $(MAKE) COMPILE_TYPE=code_coverage &&\
  7.         $(MAKE) COMPILE_TYPE=code_coverage test
  8.         lcov –capture –directory $(BIN_DIR)/$(OBJ_DIR)/code_coverage –base-directory . -o salida.out &&\
  9.         lcov –remove salida.out "*usr/include*" -o salida.out &&\
  10.         genhtml -o coverage_report salida.out
  11.         rm salida.out

Bonus makefile target: make your code pretty:

  1. .PHONY: pretty
  2. pretty:
  3.         find -L|egrep \.(cpp|h|hh)$$’|egrep -v ‘svn|_Test.cpp$$’ | xargs astyle –options=none

Remember to change your astyle options as needed.

Bonus II: Example project using gcov and gtest: gcov_gtest_sample.tar. The irony? It doesn’t use my common makefile, it predates it.

     Add A Comment

trackback Trackback URI | rsscomment Comments RSS