Detailed guide on using gcov with CMake/CDash?

我们两清 提交于 2019-11-27 11:02:02

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

Just followed the guidelines: added the files to my CMAKE_MODULE_PATH directory, added

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 gcov as a dependency for my target:

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 coverage directory of my build tree.

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.cmake to a subdirectory 'cmake_modules'. In the CMakeLists.txt file after the add_executable(foo ...) I added the following:

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

I have an ugly method to use gcovr to make a GCC Code Coverage Report without the CodeCoverage.cmake : move all *.gcno and *.gcda files to project root directory and then run gcovr

$ cd /path/to/your/project
$ mkdir build && cd build && cmake ..
$ make && make test
$ find . -type f \( -iname \*.gcno -or -iname \*.gcda \) -exec cp {} .. \;
$ cd .. 
$ gcovr -v -r .
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!