__cdecl 或 __stdcall 在 Windows 上?

__cdecl or __stdcall on Windows?(__cdecl 或 __stdcall 在 Windows 上?)

本文介绍了__cdecl 或 __stdcall 在 Windows 上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 Windows 开发一个 C++ 库,它将作为 DLL 分发.我的目标是最大化二进制互操作性;更准确地说,我的 DLL 中的函数必须可以从使用多个版本的 MSVC++ 和 MinGW 编译的代码中使用,而无需重新编译 DLL.但是,我对哪种调用约定最好感到困惑,cdeclstdcall.

I'm currently developing a C++ library for Windows which will be distributed as a DLL. My goal is to maximize binary interoperability; more precisely, the functions in my DLL must be usable from code compiled with multiple versions of MSVC++ and MinGW without having to recompile the DLL. However, I'm confused about which calling convention is best, cdecl or stdcall.

有时我会听到诸如C 调用约定是唯一保证与跨编译器相同的唯一一种"这样的陈述,这与cdecl 的解释有一些变化,特别是在如何返回值方面".这似乎并没有阻止某些库开发人员(例如 libsndfile)使用 C 调用约定在他们分发的 DLL 中,没有任何可见的问题.

Sometimes I hear statements like "the C calling convention is the only one guaranteed to be the same accross compilers", which contrasts with statements like "There are some variations in the interpretation of cdecl, particularly in how to return values". This doesn't seem to stop certain library developers (like libsndfile) to use the C calling convention in the DLLs they distribute, without any visible problems.

另一方面,stdcall 调用约定似乎是明确定义的.据我所知,所有 Windows 编译器基本上都必须遵循它,因为它是用于 Win32 和 COM 的约定.这是基于不支持 Win32/COM 的 Windows 编译器不会很有用的假设.论坛上发布的许多代码片段都将函数声明为 stdcall,但我似乎找不到一个帖子清楚地解释了为什么.

On the other hand, the stdcall calling convention seems to be well-defined. From what I've been told, all Windows compilers are basically required to follow it because it's the convention used for Win32 and COM. This is based on the assumption that a Windows compiler without Win32/COM support would not be very useful. A lot of code snippets posted on forums declare functions as stdcall but I can't seem to find one single post which clearly explains why.

有太多相互矛盾的信息,我运行的每次搜索都会给我不同的答案,这并不能真正帮助我在两者之间做出决定.我正在寻找一个清晰、详细、有争议的解释,说明为什么我应该选择一个而不是另一个(或者为什么这两个是等效的).

There's too much conflicting information out there, and every search I run gives me different answers which doesn't really help me decide between the two. I'm searching for a clear, detailed, argumented explanation as to why I should choose one over the other (or why the two are equivalent).

请注意,这个问题不仅适用于经典"函数,还有虚拟成员函数调用,因为大多数客户端代码将通过接口"与我的 DLL 接口,纯虚拟类(以下模式描述,例如 此处 和那里).

Note that this question not only applies to "classic" functions, but also to virtual member function calls, since most client code will interface with my DLL through "interfaces", pure virtual classes (following patterns described e.g. here and there).

推荐答案

我只是做了一些实际测试(使用 MSVC++ 和 MinGW 编译 DLL 和应用程序,然后将它们混合).看起来,我使用 cdecl 调用约定获得了更好的结果.

I just did some real-world testing (compiling DLLs and applications with MSVC++ and MinGW, then mixing them). As it appears, I had better results with the cdecl calling convention.

更具体地说:stdcall 的问题在于 MSVC++ 会破坏 DLL 导出表中的名称,即使在使用 extern "C" 时也是如此.例如 foo 变成 _foo@4.这仅在使用 __declspec(dllexport) 时发生,而不是在使用 DEF 文件时发生;但是,在我看来,DEF 文件维护起来很麻烦,我不想使用它们.

More specifically: the problem with stdcall is that MSVC++ mangles names in the DLL export table, even when using extern "C". For example foo becomes _foo@4. This only happens when using __declspec(dllexport), not when using a DEF file; however, DEF files are a maintenance hassle in my opinion, and I don't want to use them.

MSVC++ 名称修改带来两个问题:

The MSVC++ name mangling poses two problems:

  • 使用GetProcAddress在 DLL 上变得稍微复杂一些;
  • 默认情况下,MinGW 不会在修饰名称前添加反下划线(例如,MinGW 将使用 foo@4 而不是 _foo@4),这会使链接复杂化.此外,它还带来了看到 DLL 和应用程序的非下划线版本"的风险,这些应用程序与下划线版本"不兼容.
  • Using GetProcAddress on the DLL becomes slightly more complicated;
  • MinGW by default doesn't prepend an undescore to the decorated names (e.g. MinGW will use foo@4 instead of _foo@4), which complicates linking. Also, it introduces the risk of seeing "non-underscore versions" of DLLs and applications pop up in the wild which are incompatible with the "underscore versions".

我已经尝试了 cdecl 约定:MSVC++ 和 MinGW 之间的互操作性完美运行,开箱即用,并且名称在 DLL 导出表中保持未修饰.它甚至适用于虚方法.

I've tried the cdecl convention: interoperability between MSVC++ and MinGW works perfectly, out-of-the-box, and names stay undecorated in the DLL export table. It even works for virtual methods.

由于这些原因,cdecl 对我来说是一个明显的赢家.

For these reasons, cdecl is a clear winner for me.

这篇关于__cdecl 或 __stdcall 在 Windows 上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:__cdecl 或 __stdcall 在 Windows 上?

基础教程推荐