2010-06-29 38 views
2

我有一個庫(C++),它有一些API函數。其中之一被聲明爲__cdecl,但是從__stdcall獲取函數poiner。喜歡的東西:混合調用約定編譯錯誤

typedef int (__stdcall *Func)(unsigned char* buffer); 
//... 
int ApiFunc(Func funcPtr); //This is __cdecl since it is an 'extern "C"' library and the calling convention is not specified 

然後 - 我有一個使用這個庫,但不調用上述API或使用Func類型C++可執行項目。

改變Func調用約定到__stdcall後,我得到以下編譯錯誤:

error C2995: 'std::pointer_to_unary_function<_Arg,_Result,_Result(__cdecl *)(_Arg)> std::ptr_fun(_Result (__cdecl *)(_Arg))' : function template has already been defined c:\program files\microsoft visual studio 8\vc\include\functional

任何想法可能是什麼?

在此先感謝!

回答

1

Err ..它們不兼容。您必須在呼叫的兩側指定相同的呼叫約定。否則嘗試呼叫會炸燬機器堆棧。

1

它們兼容,在Windows至少(在Linux中有沒有__stdcall在所有...) 的問題是由錯誤,圖書館重新定義__stdcall與Linux的兼容性,如:

#ifndef __MYLIB_WIN32 
//Just an empty define for Linux compilation 
#define __stdcall 
#endif 

該exe項目包括此定義,__MYLIB_WIN32沒有在其中定義,但只在庫中。 更改上述定義爲:

#ifndef WIN32 
//Just an empty define for Linux compilation 
#define __stdcall 
#endif 

和一切工作正常。

謝謝大家。