c++flushgcov

undefined reference to `__gcov_flush'


I am trying same,

http://www.linuxforums.org/forum/suse-linux/135465-gcov-g.html

Code from the link,

#include <iostream>

using namespace std;

void one(void);
void two(void);
void __gcov_flush(void);

int main(void)
{
  int i;

  while(true)
  {
        __gcov_flush();
        cout <<  "Enter a number(1-2), 0 to exit " << endl;
        cin >> i;

        if ( i == 1 )
           one();
        else if ( i == 2 )
           two();
        else if ( i == 0 )
           break;
        else
          continue;
  }
  return 0;
}

void one(void)
{ cout << "One is called" << endl; }

void two(void)
{ cout << "Two is called" << endl; }

but for me also it gives,

test.cpp:(.text+0x1d9): undefined reference to `__gcov_flush()'
collect2: ld returned 1 exit status

Tried the followings,

g++ -fprofile-arcs test.cpp
g++ -fprofile-arcs -g test.cpp
g++ -fprofile-arcs -ftest-coverage -g test.cpp
g++ -fprofile-arcs -ftest-coverage -g test.cpp -lgcov

I have also tried the "-lgcov" & "extern void __gcov_flush(void)" as mentioned in link above. I am currently on Ubuntu12.04 and g++ 4.6

So, I want to know if there is solution for this or gcov_flush doesnt work anymore.


Solution

  • void __gcov_flush();
    

    Since the code is compiled as C++, this declares the existence of a C++ function of that name. C++ functions are subject to name mangling, so the (C++) symbol is not found in the (C) link library, and the linker (rightfully) complains about it.

    If you declare the function, declare it as a function with C linkage:

    extern "C" void __gcov_flush();
    

    This should do the trick.


    Note the comment by Paweł Bylica -- __gcov_flush() has been removed in GCC 11, you should use __gcov_dump().