在CMake项目中使用GCOV/LCOV的C++

9

我是一名从事C++项目开发的人员,这个项目的结构类似于以下内容:

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

--CMakeLists.txt

我需要控制我的测试覆盖率,为此我使用以下方式的GCOV和LCOV:

  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.
    
当我编译我的项目时,由于测试需要重新编译它们的依赖项(comms、utils等),因此会出现重复的.gcno文件,因为我不为这些依赖项生成库。如果我不使用库,我认为无法避免这种情况。
然而,当我尝试为全局项目生成索引.html(覆盖率报告)时,它不起作用。
我使用一个Shell脚本,创建与我的项目相同的文件夹结构,并将每个.gcno和.gcda复制到相应的目录中。然后执行命令lcov和genhtml,但是index.html不包含所有项目覆盖范围。
我希望能得到任何帮助。
1个回答

1

尝试在 genhtml 命令中加入 --ignore-errors 参数,例如:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接