How to pass a class by (const) reference to C++

Tim tim.dlang at t-online.de
Wed Dec 15 21:30:47 UTC 2021


On Wednesday, 15 December 2021 at 19:28:44 UTC, Jan wrote:
> C++
> ```cpp
> class Test
> {
> public:
>   __declspec(dllexport) static int var;
> };
>
> int Test::var = 42;
> ```
>
> D
> ```cpp
> extern(C++, class) struct Test
> {
>   extern (C++) export extern __gshared int var;
> }
> ```
>
> (also tried without the duplicate extern(C++) and without 
> 'export')
>
> DLL compiled with Visual Studio 2019. D DLL compiled with a 3 
> day old nightly build of DMD.
>
> DMD says:
> `error LNK2019: unresolved external symbol "public: static int 
> Test::var" (?var at Test@@2HA)`
>
> I've checked the .exp file of the DLL where the symbol is 
> defined in, and it has the exact same name there. And yes, I 
> correctly link against that DLL in general, other symbols 
> located in that DLL are linked just fine by DMD.

I can reproduce your problem. It seems to work if var is 
additionally static:

extern(C++, class) struct Test
{
    extern (C++) export extern static __gshared int var;
}

It's probably a bug, that it does not work without static. The 
documentation says "__gshared may also be applied to member 
variables and local variables. In these cases, __gshared is 
equivalent to static, except that the variable is shared by all 
threads rather than being thread local." 
(https://dlang.org/spec/attribute.html#gshared)



More information about the Digitalmars-d-learn mailing list