extern(C) and name mangling

Jacob Carlborg doob at me.com
Wed Dec 16 07:02:02 UTC 2020


On Wednesday, 16 December 2020 at 04:17:13 UTC, Mike Parker wrote:

> So what you're asking for is a way to retain the D name 
> mangling on an extern C function. The way to do that is with 
> `pragma(mangle, "new_name")`. To match the original D function 
> mangling, declare the function first without extern(C) and 
> print `func.mangleof`. Use that as the parameter to 
> pragma(mangle).
>
> I can't imagine any benefit you'd get from doing that, though.

I actually had a use case for this. An initialization function 
that is called by the C runtime, i.e. `pragma(crt_constructor)`. 
That function needs to have C calling convention. But to avoid 
any conflicts with other `extern(C)` functions I wanted to keep 
the D mangling.

https://github.com/dlang/druntime/blob/master/src/core/memory.d#L212-L232

--
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list