Search code examples
dextern

extern(C) in interfacing D to C?


When interfacing D to C, do I need extern(C) for structs and enums?

extern(C) enum XX { A, B }

extern(C) struct Z { int x; }

Do I need extern(C) above for correctly calling

extern extern(C) void f(XX a, Z b)

?


Solution

  • No, it has no effect on structs and enums. extern(C) is for functions and function pointer types primarily, and sometimes for variables shared with C too (where you might see extern extern(C) __gshared type name; - the first extern meaning it is defined externally (in other words, this definition is just for use, not storage space allocation), the second meaning defined for C, __gshared meaning a C style global variable.)

    But for the most part, you should just use it on functions and function pointers. The struct definitions don't even have to strictly match - the names don't matter at all, for example, and in some cases, contents don't either (if it is only passed and accessed through pointer, you can get away with an opaque definition - no body to the struct).