__cdecl或__stdcall在Windows上? [英] __cdecl or __stdcall on Windows?

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

问题描述

我正在开发一个用于Windows的C ++库,它将作为一个DLL进行分发。我的目标是最大化二进制互操作性;更准确地说,我的DLL中的函数必须可以从编译多个版本的MSVC ++和MinGW的代码中使用,而无需重新编译DLL。不过,我很困惑,哪个电话会议是最好的, cdecl stdcall



有时我会听到C调用约定是唯一一个保证是相同的编译器,与的解释形成对照,解释 cdecl ,特别是如何返回值。这似乎不会阻止某些图书馆开发者(例如 libsndfile )使用C语言在它们分发的DLL中的约定,没有任何明显的问题。



另一方面, stdcall 调用约定似乎定义明确。从我被告知,所有Windows编译器基本上都需要遵循,因为它是用于Win32和COM的惯例。这是基于假设没有Win32 / COM支持的Windows编译器将不是很有用。发布在论坛上的许多代码片段将功能声明为 stdcall ,但我似乎找不到一个明确说明 的单个帖子。 p>

那里有太多冲突的信息,我运行的每个搜索都给我不同的答案,这并不能帮助我在两者之间做出决定。我正在寻找一个明确,详细的论证解释,为什么我应该选择一个(或为什么两个是相等的)。



请注意,这个问题不仅适用于经典功能,还适用于虚拟成员函数调用,因为大多数客户端代码将通过interfaces与纯虚拟类(以下描述的模式(例如 here there )。

解决方案

我刚刚做了一些真实世界的测试使用MSVC ++和MinGW编译DLL和应用程序,然后混合它们)。看来,我使用 cdecl 调用约定更好的结果。



更具体地说: stdcall 是MSVC ++在DLL导出表中的名称,即使使用 externC。例如 foo 成为 _foo @ 4 。这仅在使用 __ declspec(dllexport)时发生,而不是在使用DEF文件时发生;然而,DEF文件在我看来是一个维护麻烦,我不想使用它们。



MSVC ++名称的错误提出了两个问题:




  • 使用 GetProcAddress 变得稍微复杂一些;

  • 默认情况下MinGW不会将装饰的名称(例如MinGW将使用 foo @ 4 而不是 _foo @ 4 ),这会使链接复杂化。此外,它引入了看到与下划线版本不兼容的DLL和应用程序弹出的非下划线版本的风险。



我尝试了 cdecl 约定:MSVC ++和MinGW之间的互操作性完美,开箱即用,名称在DLL中保持未装饰出口表。



由于这些原因, cdecl 对我而言是一个明显的赢家。 >

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.

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.

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).

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).

解决方案

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.

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.

The MSVC++ name mangling poses two problems:

  • 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".

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.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆