【问题标题】:Code coverage of Multiple files using Gcovr使用 Gcovr 的多个文件的代码覆盖率
【发布时间】:2018-12-24 13:07:56
【问题描述】:

我是 gcovr 的新手。我的代码中有 3 个文件,即 main.cpp、math.cpp 和 math.hpp。我使用以下命令使用 g++ 编译它

g++ -fprofile-arcs -ftest-coverage -fPIC -O0 main.cpp  math.cpp math.hpp -o math

我的代码已编译并成功运行。当我运行以下命令进行代码覆盖时

 gcovr -r .

它产生这样的输出

------------------------------------------------------------------------------
                       GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File                                       Lines    Exec  Cover   Missing
------------------------------------------------------------------------------
main.cpp                                       6       6   100%
------------------------------------------------------------------------------
TOTAL                                          6       6   100%
------------------------------------------------------------------------------ 

它只显示 main.cpp 的代码覆盖率。我也想知道其他文件的代码覆盖率。我怎样才能得到它?如果可以,请帮助我。提前致谢。

这是我的代码 main.cpp

#include "math.hpp"
int main( int argc, char **argv)
{
    Math m;
    m.Multiply(20, 50);
    m.Divide(40, 5);
}

数学.cpp

#include "math.hpp"

using namespace std;
int Math::Multiply(int Num1, int Num2){
    Mul= Num1 * Num2;
    std::cout << "Multiplication of "<<Num1<< " * " <<Num2<< " = "<< Mul<<endl;
    return Mul;
}

int Math::Divide(int Num1, int Num2){
    Div = Num1/Num2;
    std::cout << "Division of "<<Num1<< " / " <<Num2 <<" = "<< Div<<endl;
    return Div;
}

数学.hpp

#include <ctime>
#include<iostream>
class Math
{
  int Num1;
  int Num2;
  int Fact, Mul, Div, Num, i;

public:
  int Multiply(int Num1,int Num2);
  int Divide(int Num1, int Num2);
};

当运行命令 gcovr -v -r 时。下面给出

C:\Users\user\Desktop\Gcovr>gcovr -v -r .
Filters for --root: (1)
- <_sre.SRE_Pattern object at 0x01C206A0>
Filters for --filter: (1)
- DirectoryPrefixFilter(C\:\/Users\/user\/Desktop\/Gcovr\/)
Filters for --exclude: (0)
Filters for --gcov-filter: (1)
- AlwaysMatchFilter()
Filters for --gcov-exclude: (0)
Filters for --exclude-directories: (0)
Scanning directory . for gcda/gcno files...
Found 4 files (and will process 2)
Pool started with 1 threads
Processing file: C:\Users\user\Desktop\Gcovr\main.gcda
Running gcov: 'gcov C:\Users\user\Desktop\Gcovr\main.gcda --branch-counts -- 
branch-probabilities --preserve-paths --object-directory 
C:\Users\user\Desktop\Gcovr' in 'c:\users\user\appdata\local\temp\tmpyekiry'
Running gcov: 'gcov C:\Users\user\Desktop\Gcovr\main.gcda --branch-counts -- 
branch-probabilities --preserve-paths --object-directory 
C:\Users\user\Desktop\Gcovr' in 'C:\Users\user\Desktop\Gcovr'
Finding source file corresponding to a gcov data file
  currdir      C:\Users\user\Desktop\Gcovr
  gcov_fname   c:\users\user\appdata\local\temp\tmpyekiry\main.cpp.gcov
           [u'        -', u'    0', u'Source', u'main.cpp\n']
  source_fname C:\Users\user\Desktop\Gcovr\main.gcda
  root         C:\Users\user\Desktop\Gcovr
  fname        C:\Users\user\Desktop\Gcovr\main.cpp
Parsing coverage data for file C:\Users\user\Desktop\Gcovr\main.cpp
uncovered: set([])
covered:   {2: 1, 5: 1, 6: 1, 7: 4}
branches:  {5: {1: 1, 2: 0}, 6: {1: 1, 2: 0}, 7: {1: 1, 2: 0, 3: 1, 4: 0}}
noncode:   set([3])
Finding source file corresponding to a gcov data file
  currdir      C:\Users\user\Desktop\Gcovr
  gcov_fnamec:\users\user\appdata\local\temp\tmpyekiry\c~#mingw#lib#gcc#mingw32#6.3.0#include#c++#iostream.gcov
           [u'        -', u'    0', u'Source', 
u'c:/mingw/lib/gcc/mingw32/6.3.0/include/c++/iostream\n']
  source_fname C:\Users\user\Desktop\Gcovr\main.gcda
  root         C:\Users\user\Desktop\Gcovr
  fname        c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream
Parsing coverage data for file c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream
  Filtering coverage data for file c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream
Processing file: C:\Users\user\Desktop\Gcovr\math.gcda
Running gcov: 'gcov C:\Users\user\Desktop\Gcovr\math.gcda --branch-counts --branch-probabilities --preserve-paths --object-directory C:\Users\user\Desktop\Gcovr' in 'c:\users\user\appdata\local\temp\tmpyekiry'
Gathered coveraged data for 1 files

【问题讨论】:

  • 你能分享你的 main.cpp 代码吗?
  • 嗯,这很不寻常。只是为了检查:(1)您运行了math 可执行文件,并且(2)这在可执行文件旁边创建了一个math.gcda 文件?如果问题仍然存在,您能否发布在--verbose 模式下运行 gcovr 的输出?这可能表明其他文件未显示的原因。了解您使用的 gcovr 版本也很有用,当前版本是 4.1 版。
  • 您好,我使用的是 gcovr 4.1 版

标签: c++ gcovr


【解决方案1】:

问题是您将头文件math.hpp 显式传递给编译器。这不包含任何会产生目标代码的定义,这会导致以下事件序列:

  1. main.cpp 已编译。将创建一个文件 main.gcno,允许 gcov 解释覆盖率数据 main.gcda
  2. math.cpp 被编译并创建一个 math.gcno 文件。
  3. math.hpp 被编译,它用 empty 文件覆盖 math.gcno

因此,无法识别 math.cpp 的覆盖数据,并且该文件被排除在覆盖范围之外。

没有必要将math.hpp 传递给编译器,因为您的源文件#include 这个文件。如果我们从编译器调用中删除标头,我们会得到以下 gcovr 输出:

------------------------------------------------------------------------------
                           GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File                                       Lines    Exec  Cover   Missing
------------------------------------------------------------------------------
main.cpp                                       4       4   100%   
math.cpp                                       9       9   100%   
------------------------------------------------------------------------------
TOTAL                                         13      13   100%
------------------------------------------------------------------------------

(math.hpp 文件仍未列出,但它包含零个可以覆盖的语句。)

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 2013-10-07
    • 2020-02-26
    • 2014-05-07
    • 2011-09-15
    • 2018-09-30
    • 1970-01-01
    • 2012-06-30
    • 2012-03-17
    相关资源
    最近更新 更多