1

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.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • What is your OS and its version? Did you install clang from this OS or built it yourself? – osgx Jul 21 '16 at 19:40
  • 1
    ```$ uname -a : Linux pratik-ubuntu 4.2.0-27-generic #32~14.04.1-Ubuntu SMP Fri Jan 22 15:32:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux ``` I installed clang using `apt-get install`. – Pratik Bansal Jul 22 '16 at 22:21
  • Btw, forgot to mention, I am working in Ubuntu installed on VirtualBox with host Mac OSX – Pratik Bansal Jul 22 '16 at 23:31
  • This is 14.04.1 ubuntu and it should be same on hardware or in virtualbox. – osgx Jul 23 '16 at 00:44

2 Answers2

0

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
  • I think you should not compile two executables from the same source dir. – osgx Jul 22 '16 at 22:31
  • 2
    Why should that be a limitation @osgx? Our use case is compiling multiple unit tests as executables and running them one after the other. – Pratik Bansal Jul 25 '16 at 17:07
0

Your problem is that you compile the shared file (HelloAnother.cc) twice and gcov fails to understand that two copies of HelloAnother.o inside main1 and main2 need to be shared.

Instead, compile the shared code once and link it into each executable:

$ g++ --coverage -c HelloAnother.cc
$ g++ --coverage main1.cc HelloAnother.o -o main1
$ g++ --coverage main2.cc HelloAnother.o -o main2
$ ./main1 
Hello 1!!!
$ ./main2
Hello 2 !!!
$ gcov --stdout HelloAnother.gcno
        ...
        1:    4:void HelloAnother::sayHello1() {
        1:    5:  std::cout << "Hello 1!!!" << std::endl;
        1:    6:}
        -:    7:
        1:    8:void HelloAnother::sayHello2() {
        1:    9:  std::cout << "Hello 2 !!!" << std::endl;
        1:   10:}
        -:   11:
yugr
  • 19,769
  • 3
  • 51
  • 96