cmakecode-coveragepdb-filestest-coverage

How do I generate PDB files using cmake and gcc?


I want to use the OpenCppCoverage to check the coverage of my source files by my Unit Test. However, it seems that I need to have .pdb files generated for OpenCppCoverage to work.

The problem is that I am not being able to generate such files from my build. Here is my CmakeLists.txt

cmake_minimum_required(VERSION 3.17) # version can be different

project(GUnitTestingVSCode) #name of your project

add_subdirectory(googletest) # add googletest subdirectory

add_compile_definitions(TELEMETRY_DIAGNOSTICS)
add_compile_definitions(GTEST)

set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/")

include_directories(googletest/include/) # this is so we can #include <gtest/gtest.h>
include_directories(googletest/googlemock/include/gmock/) # this is so we can #include <gmock/gmock.h>
include_directories("${CMAKE_SOURCE_DIR}/"
    "${CMAKE_SOURCE_DIR}/inc/"
    "${CMAKE_SOURCE_DIR}/mocks/"
    "${CMAKE_SOURCE_DIR}/../submodule/library/inc"
    "${CMAKE_SOURCE_DIR}/../submodule/library/inc/Application"
    "${CMAKE_SOURCE_DIR}/../submodule/library/inc/Datalink/"
    "${CMAKE_SOURCE_DIR}/../submodule/library/inc/Demodulation"
    "${CMAKE_SOURCE_DIR}/../submodule/library/inc/Modulation"
    "${CMAKE_SOURCE_DIR}/../inc/Peripherals/"
    "${CMAKE_SOURCE_DIR}/../inc/Protocol/"
    "${CMAKE_SOURCE_DIR}/../inc/Diagnostics/")

file(GLOB_RECURSE SOURCES RELATIVE 
    ${CMAKE_SOURCE_DIR} "../src/Peripherals/DMA.c" "../src/Diagnostics.c"
    ${CMAKE_SOURCE_DIR} "../src/Protocol/DataLink.c"
    "src/*.cpp" "googletest/*.cpp" "mocks/*.c"
    "../submodule/library/src/*.c")

add_executable(gUnitTest ${SOURCES}) # add this executable
set(CMAKE_BUILD_TYPE RelWithDebInfo)
set_target_properties(gUnitTest PROPERTIES
    COMPILE_PDB_NAME "gUnitTestCompiled"
    COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/"
    PDB_NAME "gUnitTestLinker"
    PDB_OUTPUT_DIRECTORY  "${CMAKE_SOURCE_DIR}/build/")
target_link_libraries(gUnitTest PUBLIC gtest gmock) # link google test to this executable

However, when I build my Test folder in RelWithDebInfo mode (I also tried Debug), I get no .pdb files in the build folder. Can someone help me understand what am I doing wrong?

The compiler used is GCC 9.2.0 (mingw32)


Solution

  • I was able to figure out myself. So here is the answer if someone struggles with the same: gcc compiler is not able to generate .pdb files. What can be done is the following:

    1. Convert the debug information from gcc to pdb files using the converter called cv2pdb
    2. Use llvm/clange compiler that has support to generate .pdb
    3. Use another code coverage tool, such as gcov from gcc itself

    I decided to use option 3, and replace opencppcoverage by gcov (in fact I am using gcovr). I had to include the extra flags for coverage as mentioned above:

    -fprofile-arcs -ftest-coverage -fPIC -O0