7
votes

What is the D2 language equivalent of __declspec(dllexport)

I have the D2 DLL linkage example code up and running. Exporting functions, both in dmd's mangled name space as well as well as in standard u-mangled "C" name space, works like a charm. But I'm running into uncharted waters regarding the sharing of a (global) int Variable between DLL's as well as the main exe program... I've checked the DLL symbol table with depends22_x86 and while I made a point of using the export directive just before the Var's declaration, it does not show up in the DLL's table, while functions do. Can one export Varibles to be visible in a DLL with the Digital Mars dmd tool chain?

3
Never tried, but make sure you use __gshared.Trass3r
Many other languages/compilers don't support exporting variables. Exporting variables is a brittle thing anyway, because it fails in many scenarios (delay-linking) where functions work without problem. Why can't you write a getter/setter function for your variable instead?0xC0000022L

3 Answers

1
votes

This was a bug in the compiler (Bugzilla 10059). The following code should work now.

export __gshared int foo;
0
votes

Maybe you can do what Ralph Tandetzky says but in a static module ctor. You won’t have to explicitely call any function, all symbols will be loaded. Maybe __gshared would be appreciate, too.

0
votes

As a workaround, if exporting or importing global variables doesn't work, then write a wrapper function of the form

Type variable;

extern(C) Type * getGlobalVariable()
{
    return &variable;
}

if you want to export from D to C.