Search code examples
c++linuxcmakecode-coveragegcov

Using gcov with CMake step by step


I'm trying to run gcov on a project i'm working on, using this guide.

I followed the instruction on the guide and read a lot of posts, including the detailed guide on this site.

But I can't make it work.

The steps I followed are:

  1. My main CMakeLists.txt of my project is inside a folder called cmake. Inside this folder I created another folder called CMakeModules and put the CodeCoverage.cmake file inside.

  2. I added this code to my CMakeLists.txt:

    set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
    if(CMAKE_COMPILER_IS_GNUCXX)
    include(CodeCoverage)
    APPEND_COVERAGE_COMPILER_FLAGS()
    SETUP_TARGET_FOR_COVERAGE(NAME coverage 
    EXECUTABLE 
    DEPENDENCIES coverage)
    endif()
    
  3. I compiled using the commands:

    cmake -DCMAKE_BUILD_TYPE=DEBUG ../cmake
    # (I'm compiling from a subfolder of the master folder)
    
    make
    
    make coverage
    
  4. I have an error when I run make coverage command:

    [100%] Resetting code coverage counters to zero.
    Processing code coverage counters and generating report.
    
    Errno architecture (x86_64-linux-thread-multi-4.6.4-301.fc24.x86_64) does not match executable architecture (x86_64-linux-thread-multi-4.12.9-300.fc26.x86_64) at /usr/lib64/perl5/Errno.pm line 11.
    
    Compilation failed in require at /usr/share/perl5/vendor_perl/File/Temp.pm line 17.
    
    BEGIN failed--compilation aborted at /usr/share/perl5/vendor_perl/File/Temp.pm line 17.
    
    Compilation failed in require at /usr/bin/lcov line 66.
    
    BEGIN failed--compilation aborted at /usr/bin/lcov line 66.
    
    CMakeFiles/coverage.dir/build.make:57: set di istruzioni per l'obiettivo "CMakeFiles/coverage" non riuscito
    
    make[3]: *** [CMakeFiles/coverage] Error 255
    
    CMakeFiles/Makefile2:178: set di istruzioni per l'obiettivo "CMakeFiles/coverage.dir/all" non riuscito
    
    make[2]: *** [CMakeFiles/coverage.dir/all] Errore 2
    CMakeFiles/Makefile2:185: set di istruzioni per l'obiettivo "CMakeFiles/coverage.dir/rule" non riuscito
    
    make[1]: *** [CMakeFiles/coverage.dir/rule] Errore 2
    
    Makefile:214: set di istruzioni per l'obiettivo "coverage" non riuscito
    make: *** [coverage] Errore 2
    

My questions are:

Exactly what should I put into the EXECUTABLE slot? The path to the executable that starts the program?

The Setup of the program can be done just by that line I wrote?

I already tried the solution from other threads on this site, but I can't anything that suggest me how to make this work.


Solution

  • The EXECUTABLE option should define how to run something on your code and generate coverage data.

    For instance, it should be the instruction that run all your tests.

    If you have test defined using ctest, try something like:

    SETUP_TARGET_FOR_COVERAGE(NAME coverage 
                              EXECUTABLE ctest)
    

    (or ctest --parallel n if you have a lot of tests and more than one processor!) If you have a specific target that run some test, then try something like:

    SETUP_TARGET_FOR_COVERAGE(NAME coverage 
                              EXECUTABLE make target)
    

    Hope this helps!