2015-05-25 334 views
0

我有一個cpp代碼,我想調用一個c函數。 兩種編譯很好的.o文件,但是當鐺++的編譯執行,我收到以下錯誤:錯誤:預期在extern「C」上的非限定ID

file.cpp:74:12: error: expected unqualified-id 
    extern "C" 
     ^

在CPP文件中的代碼如下:

void parseExtern(QString str) 
{ 
#ifdef __cplusplus 
    extern "C" 
    { 
#endif 
     function_in_C(str); 
#ifdef __cplusplus 
    } 
#endif 

} 

如何我避免了錯誤?我無法用clang ++編譯c文件,我真的需要使用extern。謝謝。

+0

@Mat:這是一個_answer_!是的,這是一個簡短的問題,但是,這是一個簡單的問題:P –

+0

那麼,從CPP調用我的C函數的最佳方式是什麼?關於QString,我可以很容易地轉換爲char *。 –

+1

@LaurentCrivello:用'extern「C」'聲明函數。就像任何其他功能一樣。 –

回答

7

extern "C"鏈接規範是附加到函數聲明的東西。你不要把它放在呼叫站點。

在你的情況,你把下面的頭文件:

#ifdef __cplusplus 
    extern "C" 
    { 
#endif 
     void function_in_C(char const *); /* insert correct prototype */ 
     /* add other C function prototypes here if needed */ 
#ifdef __cplusplus 
    } 
#endif 

然後在你的C++代碼,你只需要調用它像任何其他功能。不需要額外的裝飾。

char const * data = ...; 
function_in_C(data); 
+0

工程就像一個魅力,謝謝! –

相關問題