gcov on larger projects (static libraries, ...)

azraiyl picture azraiyl · Apr 7, 2011 · Viewed 17.9k times · Source

I'm working on larger project which has the following directory layout:

Source
 MyA
  aa.cpp
  ab.cpp
  ac.cpp
 MyB
  ba.cpp
  bb.cpp
  bc.cpp
 MyTest
  testaa.cpp
  testab.cpp
  testac.cpp
  testba.cpp
  testbb.cpp
  testbc.cpp
  main.cpp
Build
 MyA
  aa.o
  ab.o
  ac.o
  libMyA.a (static library)
 MyB
  ba.o
  bb.o
  bc.o
  libMyB.a (static library)
 MyTest
  testaa.o
  testab.o
  testac.o
  testba.o
  testbb.o
  testbc.o
  MyTest (executable)

After compiling with -fprofile-arcs -ftest-coverage I execute the MyTest application inside the Build/MyTest directory. As expected there are *.gcno and *.gcda files inside the Build directory. After running gcov inside the MyTest directory different *.gcov files are produced but unfortunately not for everything inside MyA and MyB, although every function is called inside this two libraries. Tried different options but somehow I'm unable to create useful (means correct) *.gcov files with this layout.

If I copy every cpp inside one directory and repeat the steps everything works as expected and the coverage analysis is perfect.

Answer

Lasse Reinhold picture Lasse Reinhold · Nov 20, 2011
  1. You must specify source files as absolute paths to g++/gcc. Don't use relative paths with ".." or like "foo/bar.cpp", else you'll get errors like "geninfo: WARNING: no data found for XXXX".

  2. Don't include any header files on the command line to g++/gcc. Else you'll get "stamp mismatch with graph file" errors.

So, following should work when having multiple directories:

g++ --coverage -DDEBUG -g3 heyo.cpp /app/helper/blah.cpp /app/libfoo/foo.cpp -o program

./program

lcov --directory . --capture --output-file app.info

genhtml --output-directory cov_htmp app.info

Or, if you're in a Makefile that uses relative paths already, it's convenient to use:

g++ --coverage -DDEBUG -g3 $(abspath heyo.cpp helper/blah.cpp ../foo/bar/baz.cpp) -o program