Extern/scope issue
z
z at z.com
Sat Apr 3 11:10:51 UTC 2021
On Saturday, 3 April 2021 at 10:17:14 UTC, DLearner wrote:
> Does this mean D has no equivalent of C globals?
> What is the D way of doing this?
With __gshared.
If the global is defined from within another language, apparently
you'd have to do [extern(C) extern __gshared
*name*](https://dlang.org/spec/interfaceToC.html#c-globals)
It seems that the whole extern keyword can be confusing with
variables:
```d
//L is the language name
extern(L) returnType functionName(parameters); // function
implemented in another language or out of this module.
extern(L) returnType functionName(parameters) {/*...*/}//extern
only changes the name mangling and the calling rules in the
resulting assembly code.(with D it does not change anything?)
extern(L) variableType variableName; //what you did, declares a
normal variable, except that the name mangling rule is that of
the language you specified.
extern(L) extern otherQualifiersIfAny variableType variableName;
//appears to be a variable declared outside of the module, so at
link time a .obj file will have to declare a variable with this
symbol name or else the linker will error out.
```
It seems that case 4 is what you desired but i do not know if
with this module hierarchy it can/will work with dub.(it should.)
With the code as is you should be able to access both variables
from main with `testmod.xvar` and simply `xvar`.(when name
conflicts like this occur the most local is used by default,
otherwise use the full name which should be `testmain.xvar` in
this case.)
More information about the Digitalmars-d-learn
mailing list