c++ - 对 `__gcov_flush' 的 undefined reference

标签 c++ flush gcov

我也在尝试,

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() .

关于c++ - 对 `__gcov_flush' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655479/

相关文章:

c++ - 位标志作为验证例程中的输入

java - 当使用 FlushMode.AUTO 调用 session.close() 时,Hibernate 会刷新我更新的持久对象吗?

doctrine-orm - Doctrine2-在刷新之前获取实体ID

c++ - GCC6.3.0 : flag "--coverage" not functioning (no gdca files generated)

c++ - 将 gcov 与子进程和共享库一起使用时没有覆盖

c++ - 最后一个字符行编辑

c++ - 字符串函数返回奇怪的值

c++ - BOOST_MPL_ASSERT 的问题

c++ - 为什么从流中读取不需要刷新缓冲区

c - dlopen 无法使用代码覆盖工具 (lcov/gcov)