我想使用 Gcov 报告使用 CMake 编译的静态库Catch测试套件的覆盖率。
.
├── CMakeLists.txt
├── bin
├── CMakeModules
│ └── CodeCoverage.cmake
├── src
│ ├── some_function.cpp
│ ├── another_function.cpp
│ └── library_name.hpp
└── test
└── main.cpp
我按照此处的说明进行了添加CMakeModules/CodeCoverage.cmake
(请参阅上面的文件系统树)。
# CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(my_project CXX)
set(CMAKE_CXX_STANDARD 11)
# Link CMake Gcov module
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMakeModules)
include(CodeCoverage.cmake)
append_coverage_compiler_flags()
# Library
file(GLOB_RECURSE LIBRARY_SOURCE_FILES src/*.cpp)
add_library(library STATIC ${LIBRARY_SOURCE_FILES})
# Tests
file(GLOB_RECURSE TEST_SOURCE_FILES test/*.cpp)
add_executable(test ${TEST_SOURCE_FILES})
target_link_libraries(test PRIVATE library)
set_target_properties(test
PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin)
# Create a make target for the coverage executable
APPEND_COVERAGE_COMPILER_FLAGS()
SETUP_TARGET_FOR_COVERAGE_LCOV(
NAME coverage
EXECUTABLE bin/test
DEPENDENCIES library test)
如果任何测试函数失败,二进制文件bin/test
将返回一个非零值。这会导致coverage
make 目标失败:
$ make coverage
...
make[3]: *** [CMakeFiles/coverage.dir/build.make: CMakeFiles/coverage] Error 3
make[2]: *** [CMakeFiles/Makefile2: CMakeFiles/coverage.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2: CMakeFiles/coverage.dir/rule] Error 2
这些错误指向 Makefile 中调用测试二进制文件的行:
# build.make
CMakeFiles/coverage: bin/test
bin/test
关于如何解决这个问题的任何建议?