gcovr

Get coverage for files in other directories with gcvor


I am here with the problem, not getting an cpp files in gcovr output. Actually my main project consist of many sub cpp files but, they are located in different directory paths.

Directory layout:

I have a simple hello world program with the files main.cpp, helloworld.h, and helloworld.cpp in the same directory.

Notes:

Questions:


Solution

  • When I create a coverage report for the hello world program, why doesn't it show the helloworld.h file?

    Gcovr only reports a file if it contains executable code. Header files usually only contain declarations, but not function definitions. You can check whether that file was seen by running gcovr in `--verbose`` mode.

    Some GCC versions have a bug so that files with zero coverage are not reported, even if they contain executable code. See the gcvor FAQ[1] for compiler versions that fix this.

    [1]: sorry, the main gcovr.com website is down at the moment.

    When I create a coverage report for my actual software, it shows coverage for files in the main project directory. Why doesn't it show coverage for the sub files?

    Gcovr tries to filter out coverage data that is irrelevant for your project, for example coverage data for the standard library.

    So when you run gcovr without arguments like --filter or --root, gcovr will only show coverage for files within the current working directory.

    To fix this, give a root path that contains all the directories for which you want coverage, like --root ../ or --root D:/selv_ar/GCovTest/ado/cicrc_actuator_switch/ulf/src.

    Or, you can provide filters that match all files where you are interested in coverage. Note that filters are regexes, so you need to escape special characters like .. Also, use forward slashes for paths in the regex. Example in shell syntax:

    $ gcovr --root . \
        -f 'D:/selv_ar/GCovTest/ado/cicrc_actuator_switch/ulf/src/(test|core)/devices/puu_driver/' \
        -f 'D:/selv_ar/GCovTest/ado/cicrc_actuator_switch/ulf/src/test/testtools/'
    

    Make sure that you are using gcovr version 4 or later for filters to work correctly on Windows. If you want to use --root ../ style paths, you should use gcovr 4.2 (not yet released, install the development version from GitHub in the meanwhile).

    From which directory should I run gcvor? The main directory? The source directory? The workspace?

    Where possible, gcovr should be run in the same directory where GCC runs. This typically means your build directory. If GCC runs in multiple directories, try to use a parent directory of all of these. Use the --root, --filter, and search path arguments to provide suitable paths to the source code and to the gcda files.

    Where should I put the gcda files? Into the main project directory? Into the sub file directories?

    The .gcda files must be placed next to the corresponding .o object files and .gcno files that the compiler generates. This is important so that the correct source files can be found.

    If you're using cross-compilation, collecting coverage data that works can be more complicated. If you encounter problems read about GCOV_PREFIX in the “Cross-Profiling” chapter of the GCC docs and gcovr issue #259.

    Gcovr searches for the gcda files in the search paths: any (unnamed) parameters you pass to gcovr. For example, gcovr --root foo bar qux specifies two search paths bar and qux. By default, gcovr searches all subdirectories of the current working directory.