Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the leading underscore for the function names for the __stdcall and __cdecl calling conventions optional?

The __stdcall and __cdecl calling conventions states that the functions names must be preceded by an underscore:

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.

I have compiled the following code in Visual C++ (The source file had a .c extension, so I guess the source file was compiled as a C file):

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

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

int main()
{
    stdcallFunction(123);

    cdeclFunction(123);

    return 0;
}

I have noticed that the function names inside the compiled object file did not have a leading underscore:

enter image description here

Is the leading underscore for the function names for the __stdcall and __cdecl calling conventions optional?

like image 303
user8426277 Avatar asked Sep 14 '25 08:09

user8426277


2 Answers

Name decoration isn't directly related to the calling convention. For the calling convention, it's only important that caller and callee use this same convention, and the compiler can make sure they do if you provide the correct attributes in declarations.

So the name decoration is just conventional. It's sometimes useful being able to deduce how to call a function from its decorated name. Therefore, it's good practice to follow the convention when exposing functions from a library. I assume you would end up with decorated names if you dllexport these functions and link your code into a DLL.

The function decorations are not part of the C standard.

It is something defined in the ABI (Abstract Binary Interface) defined by the development environment used.

Specifically the rules reported in the question are those used by MS.

MS compiler doesn't use by default the C decoration (mangling) unless the function(s) are enclosed in EXTERN 'C' { ... } block, or a specific compiler switch is used (typically -TC).

And, as already mentioned in another answer, the MS ABI for 64bits code use a different C mangling that doesn't include the underscore prefix.

like image 31
Frankie_C Avatar answered Sep 17 '25 00:09

Frankie_C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!