如何更改 MinGW gcc 中的默认调用约定

How to change the default calling convention in MinGW gcc

在 Microsoft Visual C++ 中,可以使用 /Gz 编译器选项将默认调用约定设置为 stdcall。如何在 MinGW g++ 中执行此操作?

这似乎是搬起石头砸自己脚的好方法;更好的是,IMO,将默认值明确保留为 __cdecl,并明确声明 __stdcall 函数,这样就不会造成混淆。但是,如果您决心这样做,那么阅读 GCC 在线文档是一个不错的起点;尝试使用谷歌搜索 "gcc manual",它应该带你 (e.g.) here.

确保 select 与您的 GCC 版本匹配的手册版本,然后查阅 "Function Attributes" 部分;这应该将您指向 -mrtd 选项作为您想要做的事情的可能候选者。在选项索引中查找,然后按照参考查看类似内容:

-mrtd

Use a different function-calling convention, in which functions that take a fixed number of arguments return with the ret num instruction, which pops their arguments while returning. This saves one instruction in the caller since there is no need to pop the arguments there.

You can specify that an individual function is called with this calling sequence with the function attribute ‘stdcall’. You can also override the -mrtd option by using the function attribute ‘cdecl’. See Function Attributes.

Warning: this calling convention is incompatible with the one normally used on Unix, so you cannot use it if you need to call libraries compiled with the Unix compiler.

Also, you must provide function prototypes for all functions that take variable numbers of arguments (including printf); otherwise incorrect code is generated for calls to those functions.

In addition, seriously incorrect code results if you call a function with too many arguments. (Normally, extra arguments are harmlessly ignored.)