对"__gcov_flush"的未定义引用

undefined reference to `__gcov_flush'

本文关键字:未定义 引用 gcov flush      更新时间:2023-10-16

我正在尝试同样的事情,

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

来自链接的代码,

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

但对我来说,它也给了,

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

尝试了以下方法,

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

我也尝试了上面链接中提到的"-lgcov"和"extern void __gcov_flush(void)"。我目前使用的是 Ubuntu12.04 和 g++ 4.6

所以,我想知道是否有解决方案,或者gcov_flush不再有效。

void __gcov_flush();

由于代码被编译为C++,因此声明存在该名称的C++函数。C++函数受名称重整的影响,因此在 (C) 链接库中找不到 (C++) 符号,链接器(理所当然地)抱怨它。

如果声明函数,请将其声明为具有 C 链接的函数:

extern "C" void __gcov_flush();

这应该可以解决问题。

<小时 />

请注意 Paweł Bylica 的评论 - __gcov_flush()已在 GCC 11 中删除,您应该使用 __gcov_dump()

我修复了更改设置的此问题。

测试项目 --> 生成设置

仪器程序流程 =