在c中可以用不同的调用约定定义函数吗?

发布于 2024-11-07 00:49:27 字数 186 浏览 2 评论 0原文

int _cdecl    f (int x) { return 0; }
int _stdcall  f (int y) { return 0; }

名称修改后将是:

_f
_f@4

哪个不冲突,这在 c 中允许吗?如果不允许,为什么?

int _cdecl    f (int x) { return 0; }
int _stdcall  f (int y) { return 0; }

After name mangling will be:

_f
_f@4

Which doesn't conflict, is this allowed in c ,if not, why?

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

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

发布评论

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

评论(2

禾厶谷欠 2024-11-14 00:49:27

关键字 _cdecl_stdcall 不是 C 语言的一部分。这些是 Microsoft 扩展,之前有类似的 Borland 扩展。

在标准 C 语言中,您无法声明调用约定。显然,声明的每个函数都等效于 MS 编译器所指的“_cdecl”约定。

当您调用这两个函数时,可以使用内联汇编来区分它们。由于您使用的是特定于平台的 C 供应商扩展,因此您可能会考虑使用内联汇编。

The keywords _cdecl and _stdcall are not part of the C language. These are Microsoft extensions which were preceded by similar Borland extensions.

In the standard C language, you can't declare a calling convention. Every function declared is, obviously, equivalent to what the MS compiler refers to as the "_cdecl" convention.

It would be possible to use in-line assembly to distinguish the two functions when you call them. Because you're using a platform-specific vendor extension of C, you might consider using in-line assembly.

谢绝鈎搭 2024-11-14 00:49:27

首先,这不是破坏,而是装饰。重整是 C++ 编译器中会发生的事情,因为 C++ 最初设计为支持使用 C 样式链接工具的重载。

至于你的问题,你不能有两个同名的函数。为了应用该规则,使用未修饰的名称。

为什么会这样呢?我想这是因为装饰和调用约定不是 C 标准的一部分,并且是特定于每个编译器的。我非常确定,支持多种调用约定的 C 编译器是在 C 发明多年后才出现的。

First off, that's not mangling, that's decoration. Mangling is something that happens with C++ compilers because C++ was originally designed to to support overloading using C style link tools.

As to your question, you can't have two functions with the same name. For the purposes of applying that rule, the un-decorated name is used.

Why is this so? I'd imagine it is because decoration and calling conventions are not part of the C standard and are specific to each compiler. I'm pretty sure that C compilers supporting multiple calling conventions only came in to being a number of years after C was invented.

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