2013-10-29 341 views
4

我想同样的,未定义的参考`__gcov_flush”

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不工作了。

+0

你有没有在编译时加入'-fprofile-arcs'开关?我认为需要链接库存档。 – Sam

+0

@SAM是的..我已经尝试过。用已经过的方法更新了ans .. –

+0

将-lgcov移动到test.cpp之后。这是一个链接订购问题 – Petesh

回答

8
void __gcov_flush(); 

由于代码被编译为C++,此声明该名称的C++功能的存在。 C++函数受名称变形的影响,因此(C)链接库中找不到(C++)符号,并且链接器(正确地)抱怨它。

如果声明的功能,具有Ç联动声明为一个函数:

extern "C" void __gcov_flush(); 

这应该做的伎俩。

+0

它的工作...谢谢.. @DevSolar –

4

我修复了这个问题,改变了设置。

测试项目 - >构建设置

仪器程序流程 =

+0

他是在Linux上,而不是Mac。 – MtRoad