2013-10-29 340 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