2015-04-14 41 views
2

我有一個C++類,它的頭文件CFileMapping.cpp和CFileMapping.h我想在我的C代碼中調用C++函數CFileMapping::getInstance().writeMemory()如何從C代碼調用C++函數

即使當我裹着我的C++代碼,並在頭文件中要處理的C++代碼添加

#ifdef __cplusplus 
extern "C" 

,並在cpp文件添加
的extern「C」 ,我還是不能打電話我的功能是這樣的

CFileMapping::getInstance().writeMemory(). 

任何人都可以幫助我嗎?我想保留我的C++代碼,並能夠在需要時調用它。

回答

4

你應該爲你的C++調用創建C包裝:

extern "C" 
{ 
    void WriteMemFile() 
    { 
    CFileMapping::getInstance().writeMemory(); 
    } 
} 

// The C interface 
void WriteMemFile(); 

重要:extern "C"指定的函數使用C命名約定。

+0

感謝您的回覆。 我在.cpp文件FileMapping.cpp(.text + 0x1014)中有一個錯誤:未定義對std :: cout' 的引用。即使我添加了使用命名空間標準; extern之後「C」 –

+0

你需要使用C++編譯器進行編譯,而不是C編譯器,然後你就可以從C代碼中調用這個庫函數 –

+0

@Emb_user仔細閱讀後http://stackoverflow.com/questions/ 1041866/in-c-source-what-the-effect-of-extern-c – VolAnd