简体   繁体   English

使用 cmake 配置代码覆盖率

[英]configuring code coverage with cmake

I'm on a linux machine, trying to build a project using cmake that does out of source builds.我在一台 linux 机器上,尝试使用 cmake 构建一个项目,该项目不使用源代码构建。 For code coverage, I looked into gcov and followed a simple tutorial that generate appropriate files for a sample helloWorld.cpp program.对于代码覆盖率,我查看了 gcov 并遵循了一个简单的教程,该教程为示例helloWorld.cpp程序生成了适当的文件。 The only requirement were to compile with -fprofile-arcs -ftest-coverage flags & link with -lgcov flag, which can be done altogether with -coverage flag.唯一的要求是与编译-fprofile-arcs -ftest-coverage国旗及与链接-lgcov标志,它可以完全用做-coverage标志。

Now here comes the tricky part.现在到了棘手的部分。 I have a CMakeLists.txt wit contents as shown below:我有一个CMakeLists.txt机智内容,如下所示:

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've included appropriate compile time flags as well as linker flags too and appended them correctly.因此,我也包含了适当的编译时标志以及链接器标志,并正确附加了它们。 Another thing to note is set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) line which says that executables will be generated inside bin directory.另一件要注意的事情是set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)行,它表示将在bin目录中生成可执行文件。

Now everything works as intended except that coverage files aren't generated properly.现在,除了未正确生成覆盖文件之外,一切都按预期工作。 I follow these steps :我按照以下步骤操作:

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

and no other file is generated inside bin folder.并且在bin文件夹中没有生成其他文件。 However on more inspection I found out that there is another location build/CMakeFiles/runTest.dir/test where test.cpp.o resides initially and after the final step ./bin/runTest , two new files - test.cpp.gcda & test.cpp.gcno are generated.然而,在更多的检查中,我发现还有另一个位置build/CMakeFiles/runTest.dir/test test.cpp.o最初驻留在最后一步./bin/runTest ,两个新文件 - test.cpp.gcda &生成test.cpp.gcno

I've already tried copying test/test.cpp to build/CMakeFiles/runTest.dir/test and running gcov test.cpp but it fails stating - test.gcno:cannot open notes file我已经尝试将test/test.cpp复制到build/CMakeFiles/runTest.dir/test并运行gcov test.cpp但它未能说明 - test.gcno:cannot open notes file

A mismatch in gcov and gcc version was making this happen. gcov 和 gcc 版本不匹配导致了这种情况。 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 . gcc符号链接/usr/bin/gcc设置为最新gcc ,同为g++ ,但gcov$PATH仍然指向旧版本的gcov

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM