首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Gcovr对多个文件进行代码覆盖

使用Gcovr对多个文件进行代码覆盖
EN

Stack Overflow用户
提问于 2018-12-24 13:07:56
回答 1查看 1.3K关注 0票数 3

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

代码语言:javascript
复制
g++ -fprofile-arcs -ftest-coverage -fPIC -O0 main.cpp  math.cpp math.hpp -o math

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

代码语言:javascript
复制
 gcovr -r .

它产生这样的输出

代码语言:javascript
复制
------------------------------------------------------------------------------
                       GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File                                       Lines    Exec  Cover   Missing
------------------------------------------------------------------------------
main.cpp                                       6       6   100%
------------------------------------------------------------------------------
TOTAL                                          6       6   100%
------------------------------------------------------------------------------ 

它只显示了main.cpp的代码覆盖率。我想知道其他文件的代码覆盖率,too.How,我能得到它吗?如果您提前can.Thanks,请帮助我。

这是我的代码main.cpp

代码语言:javascript
复制
#include "math.hpp"
int main( int argc, char **argv)
{
    Math m;
    m.Multiply(20, 50);
    m.Divide(40, 5);
}

Math.cpp

代码语言:javascript
复制
#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;
}

math.hpp

代码语言:javascript
复制
#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时。如下所示

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-25 14:54:08

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

  1. main.cpp是编译的。创建一个文件main.gcno,允许gcov解释覆盖数据main.gcda
  2. math.cpp是编译的,它创建了一个math.gcno文件。
  3. math.hpp是编译的,它用空文件覆盖math.gcno

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

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

代码语言:javascript
复制
------------------------------------------------------------------------------
                           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文件仍未列出,但包含的语句为零。)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53913900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档