0

I'm confused about how to use gcov. I've got a cmake project that has two test executables which use googletest. I've added the required flags to my cmake script:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -g -O0")

And I've recompiled my code (using CLion and g++ 10.1).

Now I switch to the build directory and manually run both test suites. This, according to the docs should generate some files that can be used to generate the coverage report. Now I should be able to run

gcovr . 

from the root of the build tree (right?), however the output is this:

(base) ciaran@DESKTOP-K0APGUV:/mnt/d/libOmexMeta/cmake-build-release-wsl-ubuntu1804-gcc101$ gcovr .
------------------------------------------------------------------------------
                           GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File                                       Lines    Exec  Cover   Missing
------------------------------------------------------------------------------
------------------------------------------------------------------------------
TOTAL                                          0       0    --%
------------------------------------------------------------------------------

Any idea what I've doing wrong?

#Edit Also running

gcovr -r . --html --html-details -o example-html-details.html

Works, but generate an empty report

CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
  • Did you check that all the source files actually were recompiled? You may have to `make clean` or the equivalent. – Nate Eldredge Jul 03 '20 at 17:53
  • Yes, I deleted the build directory, recompiled and then ran `gcovr` and I still get an empty html doc. – CiaranWelsh Jul 03 '20 at 18:03
  • This isn't the problem in this case, but when debugging why gcovr doesn't show your files, running it in `--verbose` mode can be helpful. Typically, the problem is either that gcovr is confused about path names and thinks your source code isn't part of your project, or it doesn't manage to invoke gcov properly. Please also note that gcovr hasn't yet been tested with GCC 10 (the test suite still runs on GCC 5…) – amon Jul 03 '20 at 19:06
  • I'm using gcov-10, which I assumed was tested with gcc-10. Still playing around this this but helpful to know there is a verbose mode! – CiaranWelsh Jul 03 '20 at 19:35
  • yes, gcov-10 is a part of GCC 10. But gcov**r** is a third party project – amon Jul 03 '20 at 19:43

1 Answers1

1

You have to give the source files:

-r , --root

The root directory of your source files. Defaults to ‘.’, the current directory. File names are reported relative to this root.

If you build and run out of sources dir it may fail to find what it needs.

Manuel
  • 2,526
  • 1
  • 13
  • 17
  • almost: gcovr must also be invoked from the directory in which the compiler was executed. For cmake, this means that a correct gcovr invocation typically looks like `cd build && gcovr --root ..` – amon Jul 03 '20 at 19:03
  • @amon that's what I'm suggesting, the `-r` parameter must have the sources dir, not `.` if the build is in another dir, as he says. – Manuel Jul 03 '20 at 19:09
  • Thx @Manuel, that helped! THe important thing is that `-r` must not point to the build folder, but to the real `src` folder. Very interesting. – Jan May 19 '22 at 23:16