Detailed Guide on Using Gcov with Cmake/Cdash

Detailed guide on using gcov with CMake/CDash?

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.

Using gcov with CMake step by step

The EXECUTABLE option should define how to run something on your code and generate coverage data.

For instance, it should be the instruction that run all your tests.

If you have test defined using ctest, try something like:

SETUP_TARGET_FOR_COVERAGE(NAME coverage 
EXECUTABLE ctest)

(or ctest --parallel n if you have a lot of tests and more than one processor!)
If you have a specific target that run some test, then try something like:

SETUP_TARGET_FOR_COVERAGE(NAME coverage 
EXECUTABLE make target)

Hope this helps!

Using Gcov with CMake and Catch

According to implementation of SETUP_TARGET_FOR_COVERAGE_LCOV command, it passes the whole content of EXECUTABLE clause to the COMMAND clause of the add_custom_target. The latter accepts a shell command line, so you may create a command line which runs your tests but always returns zero. E.g. that way:

EXECUTABLE bin/test || /bin/true

configuring code coverage with cmake

A mismatch in gcov and gcc version was making this happen. gcc symlink /usr/bin/gcc was set to newest gcc, same for g++ but gcov in $PATH was still pointing to older version of gcov.

How to get coverage for tests with CMake and Catch2

I believe you forgot to add appropriate flags

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")


Related Topics



Leave a reply



Submit