我使用Linux和我有以下文件:未定義的引用函數
main.c, main.h
fileA.c, fileA.h
fileB.cpp, fileB.h
F1()
在fileB.h
聲明和fileB.cpp
定義的功能。我需要使用的功能fileA.c
,所以我聲明瞭功能
extern void F1();
在fileA.c
。
然而,在編譯過程中,我得到了錯誤
fileA.c: (.text+0x2b7): undefined reference to `F1'
有什麼不對?
謝謝。
ETA:多虧了我已經收到了答案,我現在有以下幾點:
在fileA.h,我有
#include fileB.h
#include main.h
#ifdef __cplusplus
extern "C"
#endif
void F1();
在fileA.c,我有
#include fileA.h
在fileB.h,我有
extern "C" void F1();
在fileB.cpp,我有
#include "fileB.h"
extern "C" void F1()
{ }
不過,我現在有錯誤
fileB.h: error: expected identifier or '(' before string constant
所以現在我有extern「C」void F1();和extern「C」void F1(){}分別在我的fileB.h和fileB.cpp中,還有extern「C」void F1();在fileA.c中。但是在編譯期間,我得到了錯誤「fileB.h:error:expected identifier」或「(」在字符串常量之前「,」fileA.c:錯誤:預期的標識符或「(」在字符串常量之前「,還有」fileA.c :warning:隱式聲明函數「F1」。「 – Rayne
@Rayne:糟糕,因爲C代碼[不會接受它](http://www.ideone.com/EB3Iz)。 –
@Rayne:看看我的編輯。 –