GCOV是GCC自带的代码覆盖工具,GCOV。
CMakeLists.txt
cmake_minimum_required(VERSION 3.0) project(gtest_sample C CXX) # cmake set info set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 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 "${PROJECT_SOURCE_DIR}/bin" ) set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin" ) include_directories("${PROJECT_SOURCE_DIR}/include") link_directories(${PROJECT_SOURCE_DIR}/bin) add_subdirectory(utils) add_subdirectory(test)
find -name "*.gcno" ./build/test/CMakeFiles/gtest_sample.dir/main.cc.gcno ./build/utils/CMakeFiles/dcl_utils.dir/utils_logging.cc.gcno ./build/utils/CMakeFiles/dcl_utils.dir/utils_gtest.cc.gcno
find -name "*.gcda" ./build/test/CMakeFiles/gtest_sample.dir/main.cc.gcda ./build/utils/CMakeFiles/dcl_utils.dir/utils_gtest.cc.gcda ./build/utils/CMakeFiles/dcl_utils.dir/utils_logging.cc.gcda
export GCOV_PREFIX=$PWD export GCOV_PREFIX_STRIP=9
再运行 gtest_sample (可以将gtest_sample拷贝到其他目录如 ~/tmp/)之后:
ls -lh total 7.0M -rwxr-xr-x 1 hxf0223 hxf0223 2.6M May 5 11:04 gtest_sample -rw-r--r-- 1 hxf0223 hxf0223 4.3M May 5 11:04 libdcl_utils.a -rw-r--r-- 1 hxf0223 hxf0223 4.6K May 5 11:10 main.cc.gcda -rw-r--r-- 1 hxf0223 hxf0223 134K May 5 11:10 utils_gtest.cc.gcda -rw-r--r-- 1 hxf0223 hxf0223 2.4K May 5 11:10 utils_logging.cc.gcda
TODO:
TODO: