.gcda files don't merge on multiple runs

2019-07-27 04:03发布

问题:

I have two main functions that use a common C++ class.

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;
}

Now I compile two executables: 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

Now, I run ./main

Hello 1!!!

When I rerun ./main

Hello 1!!!

profiling: /media/sf_ubuntu-shared/test-profiling/main.gcda: cannot map: Invalid argument profiling: /media/sf_ubuntu-shared/test-profiling/HelloAnother.gcda: cannot map: Invalid argument

One second run, I get this error (above) in trying to create/merge .gcda files.

Now, If I try to run ./main2

Hello 2 !!!

profiling: /media/sf_ubuntu-shared/test-profiling/HelloAnother.gcda: cannot map: Invalid argument

When I generate the code coverage report, the call to second function doesn't show up as if the call wasn't made.

Can anyone help me debug this issue pls? The issue seems to be related to merging of .gcda files on multiple runs, but not sure how to solve it.

I also tried clang-3.5 but with same results.

回答1:

After a lot of searching and trial/error this is what works for me:

  1. Compile first executable, run it. This generates HelloAnother.gcda and main.gcda files.
  2. Execute lcov --gcov-tool=gcov-4.4 --directory . --capture --output-file coverage.main.info
  3. rm -rf *.gcda; rm -rf *.gcno
  4. Compile second executable (main2.cpp), run it. This generates another HelloAnother.gcda and a main2.gcda file.
  5. Execute lcov --gcov-tool=gcov-4.4 --directory . --capture --output-file coverage.main2.info
  6. Now to generate nice looking html report do: genhtml -o coverage coverage.main.info coverage.main2.info