我遇到了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()」也會出現同樣的錯誤。
我明白從你們那裏
你應該發佈確切的錯誤消息,而不是等等等等。 –