extern(C) and mangling type names

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 14 20:49:07 PST 2017


On 2/14/2017 7:40 PM, Mike Parker wrote:
> So, should extern(C) turn off mangling for types?

In C, it is common to have opaque types like `struct Foo;` all over the place, 
and maybe `struct Foo { ... };` appear in multiple times. This works in C.

It doesn't work in D, even if the name is set as `extern (C)`. That's because 
there is no C symbol table in the compiler, it's still a D symbol and follows D 
lookup rules. Thus, repeatedly declaring `struct Foo` will result in collisions.

The solution is to declare `struct Foo` in one file, and have the others import 
that file.


More information about the Digitalmars-d mailing list