2013-12-18 69 views
0

我遇到了GCC編譯器的一個神祕的情況。所以,我有以下文件:GCC:使用舊C代碼時的鏈接器錯誤

//main.cpp

#include "mleak_cpp.h" 
int main(int argc, char* argv[]) 
{ 
    foo(); 
    __memory_malloc(10,"hello",5); 
return 0; 
} 

//mleak_cpp.h

...... 
void foo(void); 
void* __memory_malloc(size_t size, const char* file, int line); 

//mleak_cpp.cpp

//definitions of the functions; 
void foo(void){printf("foo\n"); 

void* __memory_malloc(size_t size, const char* file, int line){ 
    printf("%s,%d\n",file,line); 
    InitDoubleLinkList(); 

    void* p = malloc(size); 
    if(p == NULL) 
    return NULL; 

    __DuLinkList* pListNode; 
    pListNode = (__DuLinkList*)malloc(sizeof(__DuLinkList)); 

    pListNode->address = p; 
    pListNode->memorysize = size; 
    pListNode->file = file; 
    pListNode->line = line; 
    InsertBlockToList(pListNode); 
    return p; 
} 

對於某種原因,對void foo(void)的調用沒有問題,但對「__memory_malloc」的調用卻出現鏈接器錯誤,「undefined reference」等等。導致不同行爲的兩個函數之間有什麼區別?

我嘗試添加「外部C」到「的#include」指令,所以main.cpp中寫道:

extern "C"{ 
    #include "mleak_cpp.h" 
} 

和職能的聲明之前添加關鍵字「外部」,而這一次調用「foo()」也會出現同樣的錯誤。

我明白從你們那裏

+0

你應該發佈確切的錯誤消息,而不是等等等等。 –

回答

3

你將extern "C"放錯了地方的任何幫助。

如果main.c是一個真正的C文件,並mleak_cpp.cpp是真正的C++函數,那麼你就需要把extern "C"提前__memory_malloc()的定義,像這樣:

extern "C" void* __memory_malloc(size_t size, const char* file, int line){ 
    // ... 
} 

如果你把extern "C"mleak_cpp.h文件,它需要謹慎:

#ifdef __cplusplus 
    extern "C" { 
#endif 

    /* ... body of header ... */ 

#ifdef __cplusplus 
    } 
#endif 

而且,目前還不清楚爲什麼foo作品在你上面的例子,如果一個文件調用__foo(),但其他文件定義爲foo()。我假設更多的是在玩,比如你的問題中的編輯錯誤。

+0

「主」應該是一個cpp文件,以防萬一。對不起,錯字。是什麼導致「foo」和「memory_malloc」的行爲有所不同呢? – user3109672

+0

更新:好吧..然後忘了關於富。同事建議說「__」可能是原因。儘管我懷疑它,但我確實嘗試了一下,這就解釋了主要調用「__foo」的原因。 – user3109672

1

extern "C"適用於C++,而不是C,並告訴它該函數的名稱不應該被破壞。在C代碼中,你永遠不應該看到這一點。一般情況下,你把它的頭文件,你保護它,就像這樣:

#ifdef __cplusplus 
extern "C" { 
#endif 

/* C and C++ compatible header file body here */ 

#ifdef __cplusplus 
} /* end extern "C" */ 
#endif 

如果你這樣做,雖然,你需要在這兩個C和C++文件中的頭文件,從而使C++編譯器知道使用C鏈接。

您可以將extern "C"放在C++的函數定義的前面,而不要將它放在標題外,但這隻適用於僅在C代碼中包含標題的情況,所以建議按照我指出的方式進行在上面。