Setting code coverage using cmake

I work on a Linux machine, trying to build a project using cmake, which comes out of the original builds. To cover the code, I looked at gcov and followed a simple guide that generates the appropriate files for the helloWorld.cpp sample. The only requirement was to compile with the -fprofile-arcs -ftest-coverage flags and link with the -lgcov flag, which can be done with the -coverage flag in -coverage .

Now here is the hard part. I have the content of CMakeLists.txt wit as shown below:

 cmake_minimum_required (VERSION 2.8) project (SomeName) SET(GCC_COVERAGE_COMPILE_FLAGS "-g -O0 -coverage -fprofile-arcs -ftest-coverage") SET(GCC_COVERAGE_LINK_FLAGS "-coverage -lgcov") SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" ) SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" ) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) include_directories(include) set(CATCH_HEADER_PATH ${PROJECT_SOURCE_DIR}/test/catch.hpp) add_executable(runTest ${PROJECT_SOURCE_DIR}/test/test.cpp ${CATCH_HEADER_PATH}) 

So, I turned on the corresponding compile-time flags, as well as the linker flags, and added them correctly. Another note: the set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) line set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) , which says that executable files will be generated inside the bin directory.

Now everything works as intended, except that coverage files are not generated properly. I do the following:

 mkdir build && cd build cmake .. make -j4 ./bin/runTest 

and no other file is created inside the bin folder. However, during an additional check, I found that there is another location build/CMakeFiles/runTest.dir/test , where test.cpp.o is located initially, and after the last step ./bin/runTest two new files are test.cpp.gcda - test.cpp.gcda and test.cpp.gcno .

I already tried to copy test/test.cpp to build/CMakeFiles/runTest.dir/test and run gcov test.cpp , but it did not answer - test.gcno:cannot open notes file

+3
source share
1 answer

The mismatch in the versions of gcov and gcc led to this happening. gcc symbolic link /usr/bin/gcc was installed on the latest gcc , the same for g++ , but gcov in $PATH still pointed to an older version of gcov .

+1
source

Source: https://habr.com/ru/post/1245452/


All Articles