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
Std::Shared_Ptr Initialization: Make_Shared<Foo>() VS Shared_Ptr<T>(New Foo)
Function in C++ Returns by Value or by Reference
How to Update a Printed Message in Terminal Without Reprinting
Populate an Array Using Constexpr at Compile-Time
Debugging Information Cannot Be Found or Does Not Match Visual Studio'S
How to Alloc a Executable Memory Buffer
Capture Output of Spawned Process to String
Why How to Use 'Std::Move' on a 'Const' Object
Has Anyone Ever Had a Use for the _Counter_ Pre-Processor MACro
Could I Ever Want to Access the Address Zero
How to Read/Write a Struct in Binary Files
On Local and Global Static Variables in C++
Correctly Reading a Utf-16 Text File into a String Without External Libraries