2

I have the following project structure:

├── app
│   ├── CMakeLists.txt
│   └── tcpstream
│       ├── CMakeLists.txt
│       ├── include
│       │   └── TCPAcceptor.h
│       └── src
│           └── TCPAcceptor.cpp
├── cmake
│   └── CodeCoverage.cmake
├── CMakeLists.txt
└── test
    ├── CMakeLists.txt
    └── TestTCPAcceptor.cpp


Contents of main CMakeLists.txt:

...

if(BUILD_TESTING)  
  find_package(GTest 1.8.0 EXACT REQUIRED COMPONENTS gtest gmock gtest_main)
  if(NOT GTest_FOUND)
    message(FATAL_ERROR "Couldn't find gtest")
  endif()
  enable_testing()
  include(CodeCoverage)
  append_coverage_compiler_flags()
  set(COVERAGE_LCOV_EXCLUDES "${CMAKE_CURRENT_LIST_DIR}/test/*")
  add_subdirectory(${CMAKE_SOURCE_DIR}/test)
endif()

...

Contents of test/CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
add_executable(TestTCPAcceptor
TestTCPAcceptor.cpp
    $<TARGET_PROPERTY:tcpstream,SOURCE_DIR>/include/TCPAcceptor.h
)

target_include_directories(TestTCPAcceptor
    PRIVATE
    $<TARGET_PROPERTY:tcpstream,SOURCE_DIR>/include
)

target_link_libraries(TestTCPAcceptor
    PUBLIC
    tcpstream
    GTest::gtest_main
    GTest::gtest
    GTest::gmock         
)

add_test(NAME TestTCPAcceptor COMMAND TestTCPAcceptor)

# Set up coverage for test executables
setup_target_for_coverage_gcovr_html(
    NAME coverage                 
        EXECUTABLE  TestTCPAcceptor  
        
        DEPENDENCIES  
        TestTCPAcceptor       
)

Code coverage report is also generated. But it reports for test file TestTCPAcceptor.cpp which is I don't want. I want coverage for source file (TCPAcceptor.cpp) instead. Where should I change to generate the report for source file and not for the test files. Any help would be appreciated.

Following is the html code coverage report. enter image description here

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Vikash
  • 67
  • 1
  • 7

1 Answers1

1

Resolved myself. The following flags were missing in the rootCMakeLists.txt. Make sure to put the flags before source subdirectory.

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}" )
Vikash
  • 67
  • 1
  • 7
  • 1
    `append_coverage_compiler_flags()` should do the trick - it is equivalent to all the required flags AFAIK. – Quarra Aug 13 '21 at 06:51