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

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

问题描述

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



有时我会听到类似C调用约定是唯一一个保证与编译器相同的语句,这与 cdecl 的解释有一些变化,特别是如何返回值< a>。这似乎并未阻止某些库开发人员(如 libsndfile )在其分发的DLL中使用C调用约定,而没有任何可见的问题。



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

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



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

解决方案

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



更具体地说: c $ c> stdcall 是MSVC ++在DLL导出表中修改名称,即使使用 externC。例如 foo 变为 _foo @ 4 。这只发生在使用 __ declspec(dllexport)时,而不是使用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天全站免登陆