How to use gcovr with source files outside the current/build/run directory?

code-coverage, gcov, gcovr

Solution

What I understood from your code up there is that you made everything and ran the program but you are still inside build directory where the object file resides.

So, what you need to understand is:

gcovr -v -r <path>

this `-r` flag takes the root directory, which means the parent directory inside which the source and object directory resides. So that it can trace them both and generate coverage data and whatever else you want it to generate. Try doing that and it will work.

For your understanding:

The `.gcno` files that gets generated after compilation is just the flowchart kind of data for that particular source file. Then later when you execute the program, a `.gcda` file gets generated for each source file. This file contains real coverage data, but for gcovr all these three files are necessary (.gcno, .gcda, sourceFile)

Hope it helped. :)

update: Back with the work around You can supply your coverage data location as a pure arg (no option) and point the root to your sources.

gcovr .../path/To/GCDA -r .../path/to/src/ [rest desired flags]

This will solve your problem for sure. Worked for me in covering my projects.

Problem

``` mkdir -p /tmp/build && cd /tmp/build && mkdir -p /tmp/src && echo "int main(){return 0;}" > /tmp/src/prog.c && gcc --coverage -o prog /tmp/src/prog.c && ./prog && gcovr -v -r . ``` will output an empty report. ``` Scanning directory . for gcda/gcno files... Found 2 files (and will process 1) Processing file: /tmp/build/prog.gcda Running gcov: 'gcov /tmp/build/prog.gcda --branch-counts --branch-probabilities --preserve-paths --object-directory /tmp/build' in '/tmp/build' Finding source file corresponding to a gcov data file currdir /tmp/build gcov_fname #tmp#src#prog.c.gcov [' -', ' 0', 'Source', '/tmp/src/prog.c\n'] source_fname /tmp/build/prog.gcda root /tmp/build fname /tmp/src/prog.c Parsing coverage data for file /tmp/src/prog.c Filtering coverage data for file /tmp/src/prog.c Gathered coveraged data for 0 files ------------------------------------------------------------------------------ GCC Code Coverage Report Directory: . ------------------------------------------------------------------------------ File Lines Exec Cover Missing ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ TOTAL 0 0 --% ------------------------------------------------------------------------------ ``` However if I manually run ``` gcov /tmp/build/prog.gcda --branch-counts --branch-probabilities --preserve-paths --object-directory /tmp/build ``` I get correct results ``` File '/tmp/src/prog.c' Lines executed:100.00% of 1 No branches No calls Creating '#tmp#src#prog.c.gcov' ``` It seems that `gcovr` did not extract the coverage from the otherwise correct `gcov` output. This only happens if the source file is outside the current directory (same as build directory, same as output directory, same as run directory), and `gcc` ics called with an absolute path to the source file. How can I fix this? Edit Fixed in upstream gcovr for relative paths, but looks like a bug for absolute paths. See https://github.com/gcovr/gcovr/issues/169.

Original source