用cmake配置代码覆盖率 [英] configuring code coverage with cmake
问题描述
我在Linux机器上,尝试使用cmake进行项目,而该项目没有源代码.对于代码覆盖率,我研究了gcov并遵循了一个简单的教程,该教程为示例helloWorld.cpp
程序生成了适当的文件.唯一的要求是使用-fprofile-arcs -ftest-coverage
标志&与-lgcov
标志链接,完全可以与-coverage
标志链接.
I'm on a linux machine, trying to build a project using cmake that does out of source builds. For code coverage, I looked into gcov and followed a simple tutorial that generate appropriate files for a sample helloWorld.cpp
program. The only requirement were to compile with -fprofile-arcs -ftest-coverage
flags & link with -lgcov
flag, which can be done altogether with -coverage
flag.
现在这是棘手的部分.我有一个CMakeLists.txt
机智内容,如下所示:
Now here comes the tricky part. I have a CMakeLists.txt
wit contents 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})
因此,我也包括了适当的编译时标志和链接器标志,并正确地附加了它们.还要注意的另一件事是set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
行,该行表示将在 bin
目录中生成可执行文件.
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.
现在,除了没有正确生成coverage文件之外,其他所有东西都按预期工作.我按照以下步骤操作:
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
,并且 bin
文件夹中没有其他文件生成.但是,经过进一步检查,我发现还有 build/CMakeFiles/runTest.dir/test
的另一个位置,test.cpp.o
最初位于该位置,而在最后一步./bin/runTest
之后,有两个新文件-test.cpp.gcda
&生成test.cpp.gcno
.
and no other file is generated inside bin
folder. 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.
我已经尝试将test/test.cpp
复制到 build/CMakeFiles/runTest.dir/test
并运行gcov test.cpp
,但是它无法说明-test.gcno:cannot open notes file
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
推荐答案
gcov和gcc版本不匹配导致了这种情况. gcc
symlink /usr/bin/gcc
设置为最新的gcc
,与g++
相同,但$PATH
中的gcov
仍指向gcov
的旧版本.
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
.
这篇关于用cmake配置代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!