.gcda文件不能在多次运行中合并

时间:2016-07-21 00:19:41

标签: c++ clang code-coverage profile gcov

我有两个使用常见C ++类的主要函数。

File1:main.cpp

   #include <iostream>
   #include "HelloAnother.h"

   int main() {
       HelloAnother::sayHello1();
       return 0;
   }

File2:main2.cpp

   #include <iostream>
   #include "HelloAnother.h"

   int main() {
       HelloAnother::sayHello2();
       return 0;
   }

File3:HelloAnother.h

   #pragma once
    class HelloAnother {
        public:
         static void sayHello1();
         static void sayHello2();
    };

File4:HelloAnother.cpp

#include <iostream>
#include "HelloAnother.h"
void HelloAnother::sayHello1() {
    std::cout << "Hello 1!!!" << std::endl;
}

void HelloAnother::sayHello2() {
    std::cout << "Hello 2 !!!" << std::endl;
}

现在我编译了两个可执行文件: clang-3.8 -o main -fprofile-arcs -ftest-coverage --coverage -g -fPIC -lstdc++ main.cpp HelloAnother.cpp

clang-3.8 -o main2 -fprofile-arcs -ftest-coverage --coverage -g -fPIC -lstdc++ main2.cpp HelloAnother.cpp

现在,我运行./main

  

你好1 !!!

当我重新运行./main

  

你好1 !!!

     

profiling:/media/sf_ubuntu-shared/test-profiling/main.gcda:无法映射:参数无效   分析:/media/sf_ubuntu-shared/test-profiling/HelloAnother.gcda:无法映射:无效参数

一秒钟运行,我在尝试创建/合并.gcda文件时遇到此错误(上面)。

现在,如果我尝试运行./main2

  

你好2 !!!

     

profiling:/media/sf_ubuntu-shared/test-profiling/HelloAnother.gcda:无法映射:参数无效

当我生成代码覆盖率报告时,对第二个功能的调用不会显示,就好像没有拨打电话一样。

任何人都可以帮我调试这个问题吗?这个问题似乎与多次运行时合并.gcda文件有关,但不确定如何解决它。

我也尝试过clang-3.5但结果相同。

1 个答案:

答案 0 :(得分:0)

经过大量搜索和试用/错误后,这对我有用:

  1. 编译第一个可执行文件,运行它。这会生成HelloAnother.gcda和main.gcda文件。
  2. 执行import UIKit private let reuseIdentifier = "Cell" class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { override func viewDidLoad() { super.viewDidLoad() collectionView?.backgroundColor = UIColor.brownColor() self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) } // MARK: UICollectionViewDataSource override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) cell.backgroundColor = UIColor.blueColor() return cell } }
  3. rm -rf * .gcda; rm -rf * .gcno
  4. 编译第二个可执行文件(main2.cpp),运行它。这会生成另一个 HelloAnother.gcda和一个main2.gcda文件。
  5. 执行lcov --gcov-tool=gcov-4.4 --directory . --capture --output-file coverage.main.info
  6. 现在生成漂亮的html报告:lcov --gcov-tool=gcov-4.4 --directory . --capture --output-file coverage.main2.info