c++ - 逐步将 gcov 与 CMake 结合使用

标签 c++ linux cmake code-coverage gcov

我正在尝试在我正在处理的项目上运行 gcov,使用 this guide .

我按照指南上的说明阅读了很多帖子,包括 detailed guide在这个网站上。

但我无法让它发挥作用。

我遵循的步骤是:

  1. 我的项目的主要 CMakeLists.txt 位于名为 cmake 的文件夹内。在此文件夹中,我创建了另一个名为 CMakeModules 的文件夹,并将 CodeCoverage.cmake 文件放入其中。

  2. 我将此代码添加到我的 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. 我使用以下命令进行编译:

    cmake -DCMAKE_BUILD_TYPE=DEBUG ../cmake
    # (I'm compiling from a subfolder of the master folder)
    
    make
    
    make coverage
    
  4. 运行 makecoverage 命令时出现错误:

    [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
    

我的问题是:

我到底应该将什么放入 EXECUTABLE 插槽中?启动程序的可执行文件的路径?

程序的安装只要我写的那一行就可以完成吗?

我已经尝试过该网站上其他线程的解决方案,但我无法提供任何建议我如何完成这项工作。

最佳答案

EXECUTABLE 选项应定义如何在代码上运行某些内容并生成覆盖率数据。

例如,它应该是运行所有测试的指令。

如果您使用 ctest 定义了测试,请尝试以下操作:

SETUP_TARGET_FOR_COVERAGE(NAME coverage 
                          EXECUTABLE ctest)

(或者 ctest --parallel n 如果您有大量测试和多个处理器!) 如果您有运行某些测试的特定目标,请尝试以下操作:

SETUP_TARGET_FOR_COVERAGE(NAME coverage 
                          EXECUTABLE make target)

希望这有帮助!

关于c++ - 逐步将 gcov 与 CMake 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50720385/

相关文章:

c++ - 特定窗口中的鼠标钩子(Hook)

mysql - 将 mysql 数据库从 linux 移动到 windows

Linux bash : Use awk(substr) to get parameters from file input

makefile - 找不到 CMAKE_C_COMPILER

visual-studio - 为什么 MSBuild 将 .tlog 文件生成到 CMakeFiles/CompilerIdC 中,我该如何让它停止?

c++ - 如何输入十六进制值而不是字符作为 boost::asio::buffer 的输入

c++ - std::vector<std::pair<const int, const int>>::clear() VS 2010 无编译

c++ - 使用两个 cpp 文件时使用未声明的标识符

linux - 使用 ssh 运行带参数的远程 bash 脚本

c++ - 源是通过 add_library 命令 PUBLIC 还是 PRIVATE 添加到库中的?