extern(C) and name mangling

Mike Parker aldacron at gmail.com
Wed Dec 16 04:56:35 UTC 2020


On Wednesday, 16 December 2020 at 04:45:34 UTC, Dave P. wrote:

>
> Oh interesting, so I only need extern(C) for declaring symbols 
> I’m linking to and
> for symbols I want to export to C. I had sort of assumed that D 
> might have
> different calling conventions for different things, but that 
> makes things a lot easier.
>

Not so fast! :-)

extern(C) does affect the calling convention on Windows x86. 
There's also extern(Windows), which changes a function to the 
stdcall calling convention used by the Win32 API (and OpenGL 
implementations on Windows, and a handful of other libraries). 
And there's no guarantee that as D moves to new platforms that 
there won't be other exceptions joining x86 Windows.

That's why I said I'm not sure I'd ever pass a templated function 
pointer to C. It isn't going to work on 32-bit Windows, or with 
any stdcall C function, right now, and possibly other platforms 
in the future.

So as a default, you should always be explicit with your 
extern(x) linkage attributes on functions even when you aren't 
actually linking with C, and only break that rule when it's 
necessary.


More information about the Digitalmars-d-learn mailing list