C++ 在 CMake/CDash 中使用 gcov 的详细指南?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13116488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:59:04  来源:igfitidea点击:

Detailed guide on using gcov with CMake/CDash?

c++testingcmakecode-coveragecdash

提问by Ralph Zhang

I'm using CMake with my project and set up a cdash server for continuous/nightly building. Everything works well and by setting up a crontab, we have hourly/nightly build/test results uploaded to our cdash server automatically.

我在我的项目中使用 CMake,并为连续/夜间构建设置了一个 cdash 服务器。一切正常,通过设置 crontab,我们可以将每小时/每晚的构建/测试结果自动上传到我们的 cdash 服务器。

My next step is to add test coverage report to the build. I find the document here http://www.cmake.org/Wiki/CTest:Coveragebut frankly it's a bit far from a practical guide.

我的下一步是将测试覆盖率报告添加到构建中。我在这里找到文档http://www.cmake.org/Wiki/CTest:Coverage但坦率地说,它离实用指南有点远。

Currently I've added the required flag (instead of -fprofile-arcs -ftest-coverage, I find --coveragebetter), the compilation process generates .gcno files. But then I'm stuck. The command

目前我已经添加了 required 标志(而不是-fprofile-arcs -ftest-coverage,我发现--coverage更好),编译过程生成 .gcno 文件。但后来我被卡住了。命令

make NightlyCoverage

doesn't seem to do anything. Could anybody tell me what is the next to do? The result that I want, is by doing make NightlyCoverage, coverage reports are generated and uploaded to cdash server.

似乎没有做任何事情。谁能告诉我接下来要做什么?我想要的结果是通过执行make NightlyCoverage生成覆盖率报告并将其上传到 cdash 服务器。

采纳答案by rcomblen

I've been using https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmakesuccessfully.

我一直在成功使用https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake

Just followed the guidelines: added the files to my CMAKE_MODULE_PATHdirectory, added

只是按照指导方针:将文件添加到我的CMAKE_MODULE_PATH目录中,添加

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
if(CMAKE_COMPILER_IS_GNUCXX)
    include(CodeCoverage)
    setup_target_for_coverage(${PROJECT_NAME}_coverage ${PROJECT_TEST_NAME} coverage)
endif()

in my CMakeLists.txt. I also added manually gcovas a dependency for my target:

在我的CMakeLists.txt. 我还手动添加了gcov作为目标的依赖项:

if(CMAKE_COMPILER_IS_GNUCXX)
    target_link_libraries(${PROJECT_TEST_NAME} gcov)
endif()

With this, I just type

有了这个,我只需输入

make my_project_coverage

and I get the html report in the coveragedirectory of my build tree.

我在coverage构建树的目录中获得了 html 报告。

回答by psalong

I set up my project 'foo' in the following way. Copied the cmake file from the https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmaketo a subdirectory 'cmake_modules'. In the CMakeLists.txt file after the add_executable(foo ...)I added the following:

我按照以下方式设置了我的项目 'foo'。将 cmake 文件从https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake复制到子目录“cmake_modules”。在 CMakeLists.txt 文件中add_executable(foo ...)添加以下内容后:

if(CMAKE_COMPILER_IS_GNUCXX)
LIST(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake_modules")
include(CodeCoverage)
APPEND_COVERAGE_COMPILER_FLAGS()
set(COVERAGE_LCOV_EXCLUDES 'dir1/*' 'dir2/*') // this is optional if you want to exclude some directory from the report
SETUP_TARGET_FOR_COVERAGE_LCOV(NAME foo_coverage
                              EXECUTABLE foo
                              DEPENDENCIES foo)
endif()

After cmake, build the target make make foo_coverage And open the report with index.html file in the foo_coverage folder in the build folder

cmake之后,构建目标 make make foo_coverage 并打开build文件夹下foo_coverage文件夹中带有index.html文件的报告

回答by yee

I use gcovrto make a GCC Code Coverage Reportwithout the CodeCoverage.cmake:

我过去常常在没有以下内容的情况gcovr制作GCC 代码覆盖率报告CodeCoverage.cmake

$ cd /path/to/your/project
$ mkdir build && cd build && cmake ..
$ make && make test
$ gcovr -r ../ .