gpt4 book ai didi

c - __stdcall 和 __cdecl 调用约定的函数名称的前导下划线是可选的吗?

转载 作者:行者123 更新时间:2023-12-02 03:01:37 30 4
gpt4 key购买 nike

__stdcall__cdecl 调用约定规定函数名必须在前面加上下划线:

C (__cdecl)

The same constraints apply to the 32-bit world as in the 16-bit world. The parameters are pushed from right to left (so that the first parameter is nearest to top-of-stack), and the caller cleans the parameters. Function names are decorated by a leading underscore.

__stdcall

This is the calling convention used for Win32, with exceptions for variadic functions (which necessarily use __cdecl) and a very few functions that use __fastcall. Parameters are pushed from right to left [corrected 10:18am] and the callee cleans the stack. Function names are decorated by a leading underscore and a trailing @-sign followed by the number of bytes of parameters taken by the function.

我在 Visual C++ 中编译了以下代码(源文件的扩展名为 .c,所以我猜源文件被编译为 C 文件):

void __stdcall stdcallFunction(int i)
{
int j = 12345;
}

void __cdecl cdeclFunction(int i)
{
int j = 12345;
}

int main()
{
stdcallFunction(123);

cdeclFunction(123);

return 0;
}

我注意到编译目标文件中的函数名没有前导下划线:

enter image description here

__stdcall__cdecl 调用约定的函数名称的前导下划线是否可选?

最佳答案

名称修饰与调用约定没有直接关系。对于调用约定,唯一重要的是调用方和被调用方使用相同的约定,如果您在声明中提供正确的属性,编译器可以确保它们这样做。

所以名称修饰只是约定俗成。有时能够从函数的修饰名中推断出如何调用函数是很有用的。因此,在公开库中的函数时遵循约定是一种很好的做法。我假设如果您dllexport这些函数并将您的代码链接到一个 DLL 中,您最终会得到经过修饰的名称。

关于c - __stdcall 和 __cdecl 调用约定的函数名称的前导下划线是可选的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45605895/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com