reading a global external (C) char* in D2

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Sun Apr 18 10:14:33 PDT 2010


Graham Fawcett wrote:
> Hi folks,
> 
> I'm having trouble reading an external(C) global value in D2. I think
> I'm using __gshared properly to sidestep the TLS issues, but I'm still
> getting an incorrect result.
> 
> Consider the two following programs, one in C, the other D2:
> 
>     // testc.c
>     #include <stdio.h>
>     extern char *gdbm_version;	// or #include <gdbm.h>
> 
>     int main() {
>       printf("VERSION (C): %s.\n", gdbm_version);
>     }
> 
> $ gcc -o testc testc.c -lgdbm && ./testc
> VERSION (C): GDBM version 1.8.3. 10/15/2002 (built Nov  5 2008 02:36:47).
> 
>     // testd.d
>     import std.stdio;
>     import std.conv;
> 
>     __gshared extern (C) char *gdbm_version;
> 
>     void main() {
>       string v = to!string(gdbm_version);
>       writef("VERSION (D): %s.\n", v);
>     }
> 
> $ dmd testd.d -L-lgdbm && ./testd
> VERSION (D): .
> 
> Am I doing something wrong? 
> 
> Thanks,
> Graham


Try adding a second 'extern':

   extern(C) extern __gshared char* gdbm_version;

-Lars


More information about the Digitalmars-d-learn mailing list