Search code examples
d

Export a function with a c-style name?


Such that on windows, in C, I could do like so

void (*add)(int,int) = GetProcAddress(hDlangMod, "add");
add(422,244);

Solution

  • You can add extern (C) to remove name mangling if that's what you want, be aware that it will remove some features like overloading on the D side, but if you need the function being easily accessible from C, its a great solution! You can read more about it and other techniques in https://dlang.org/spec/interfaceToC.html

    TLDR: Just define it as extern (C) void GetProcAddress(your params here) and link it appropriately, and it will work.

    Note: The same extern directive can be used to import symbols from C or export to C++, along with other languages and uses, its quite a useful tool! For example: https://dlang.org/spec/cpp_interface.html