C++ using GCOV/LCOV in a CMake project

2020-07-08 07:54发布

问题:

I'm working in a C++ Project with a structure similiar to the following:

---  /src
    |--comms
    |--utils
    |--interfaces
    …
    CMakeList.txt
--- /test
    |---test1/
              |--main.cpp
              |--CMakelists.txt

--CMakeLists.txt

I do need to control the coverage of my tests and for this purpose I use GCOV and LCOV in this way:

  1. Enable coverage flags in all CMakeLists.txt to let the generation of .gcno files.

    SET(CMAKE_CXX_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
    SET(CMAKE_C_FLAGS "-g -O0 -Wall -W -fprofile-arcs -ftest-coverage")
    SET(CMAKE_EXE_LINKER_FLAGS "-fprofile-arcs -ftest-coverage")
    
  2. Run test, generating the corresponding .gcda files.

    At this point, files gcno and gcda are located in the same directory as the corresponding .o file. I cannot move these files, because if I do it the report coverage generation doesn’t work.

  3. From the directory in which files .gcno and .gcda are located I do the following:

    lcov –c –d . –o name.info      
    
  4. Generate the HTML report by using:

    genhtml  name.info.
    

When I compile my project, I have duplicated .gcno files due to the fact that when tests are compiled they need to recompile their dependencies (comms, utils, …) because I don't generate libraries for theses dependencies. I think there is no way to avoid that if I don't use libraries.

However when I try to generate the index.html (coverage report) for the global project, it doesn't work.

I use a Shell script that creates the same folder structure of my project and copy each .gcno and .gcda to the corresponding directory. And I execute the commands lcov and genhtml, nevertheless the index.html does not include all project coverage.

I would appreciate any help.

回答1:

try to put --ignore-errors with genhtml command like that :

genhtml -o out name.info --ignore-errors source


标签: cmake gcov lcov