The code snippet was a part of CMakelists.txt.
set(HOLO_BUILD_TEST ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -DNDEBUG --coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -DNDEBUG --coverage")
#find gcov
find_program(LCOV_BIN lcov)
#fine html, used for generating html information
find_program(GENHTML_BIN genhtml)
if(LCOV_BIN AND GENHTML_BIN)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory coverage WORKING_DIRECTORY ${PROJECT_BINARY_DIR})
add_custom_target(coverage
#Create coverage files range
COMMAND ${LCOV_BIN} --rc lcov_branch_coverage=1 --no-external -c
-b ${PROJECT_SOURCE_DIR} -d ${PROJECT_BINARY_DIR} -o ${PROJECT_NAME}.info
#Add exception, then the result will not include XXXXX.cpp
COMMAND ${LCOV_BIN} --rc lcov_branch_coverage=1 -r ${PROJECT_NAME}.info
"*/${PROJECT_NAME}/XXXXX.cpp" -o ${PROJECT_NAME}.info
COMMAND ${GENHTML_BIN} --rc lcov_branch_coverage=1 ${PROJECT_NAME}.info -o ${PROJECT_NAME}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/coverage
)
else()
MESSAGE(ERROR "LCOV not be found! LCOV is used for generating test coverage report. \
Please run 'apt-get install lcov' to install lcov")
endif()
If you want to use GCOV feature, you need to follow the steps .
1.Install lcov:
sudo apt-get install lcov
2.Create a CMakeLists.txt and add the code snippet.
1)Debug mode
2)-O0 option
3.Generate enough unit test or execute files.