2010-06-29 39 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 

和一切工作正常。

谢谢大家。