1

I have setup gcovr to get code coverage on my c++ project which is quite complex in terms of hierarchy of nested static libraries.

One approach I tried was to include required compile options (-fprofile-instr-generate -fcoverage-mapping -fPIC -fprofile-arcs -ftest-coverage) and link target libraries flags (-fprofile-instr-generate) on the target for which coverage is supposed to be calculated. But this doesn't include the code from child static library. To resolve that I added the flags to children static library which worked but this doesn't work for grandchildren & later on.

How can I setup the cMake correctly to get code coverage for all the classes irrespective of their library position?

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
vkg
  • 685
  • 1
  • 8
  • 21
  • 1
    You can look at this question... https://stackoverflow.com/questions/57330684/why-does-cmake-neglects-source-files – VVish Sep 09 '19 at 11:10

1 Answers1

0

I found that I need to explicitly call add_code_coverage function for the static libraries. Finally ended up writing following wrapper method:

function(target_link_libraries_with_coverage targetName library)
    target_link_libraries(${targetName} ${library})
    add_code_coverage(${library})
endfunction()

This should be called instead of target_link_libraries when linking the libraries for which you need coverage.

Note: add_code_coverage is adding required compile options

vkg
  • 685
  • 1
  • 8
  • 21