混合调用约定会导致编译错误

发布于 2024-09-08 05:05:44 字数 702 浏览 11 评论 0原文

我有一个库(C++),它有一些 API 函数。其中之一被声明为 __cdecl,但从 __stdcall 获取函数指针。类似于:

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

然后 - 我有一个使用此库的 C++ 可执行项目,但不调用上述 API 或使用 Func 类型。

Func 的调用约定更改为 __stdcall 后,出现以下编译错误:

错误 C2995: 'std::pointer_to_unary_function<_Arg,_Result,_Result(__cdecl *)(_Arg)>; std::ptr_fun(_Result (__cdecl *)(_Arg))' :函数 模板已经有了 定义 c:\program files\microsoft Visual Studio 8\vc\include\功能

知道它是什么吗?

提前致谢!!

I have a library (C++) which has some API functions. One of them is declared as __cdecl, but gets a function poiner from __stdcall. Something like:

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

Then - I have a C++ executable project which uses this library, but doesn't call the above API or uses the Func type.

After changing the calling convention of Func to __stdcall, I get the following compilation error:

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

Any idea what could it be?

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦屿孤独相伴 2024-09-15 05:05:44

呃..他们不兼容。您必须在调用双方指定相同的调用约定。否则尝试调用将炸毁机器堆栈。

Err.. they're incompatible. You have to specify the same calling convention on both sides of the call. Otherwise attempting to call will blow up the machine stack.

多孤肩上扛 2024-09-15 05:05:44

它们是兼​​容的,至少在 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

一切正常。

谢谢大家。

They ARE compatible, in Windows at least (and in Linux there isn't __stdcall at all...)
The problem was that by mistake, the library re-defined __stdcall for compatibility with Linux, as:

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

The exe project includes this definition, and __MYLIB_WIN32 was not defined in it, but in the library only.
Changing the above definition to:

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

and everything works fine.

Thank you all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文