www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Serious extern(C) bug

reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
Have you seen this one before? Do you know a workaround? (DMD  
v2.063.2, on OX X 10.9)

file.d:

     extern(C)
     {
         int x = 0;
         void setx();
         void printx();
     }

     void main()
     {
         setx(); // sets x = 42
         writeln(x); // prints x = 0
         printx(); // prints x = 42
         x = 7;
         printx(); // prints x = 42
     }



file.c:

     #include <stdio.h>

     extern int x;

     void setx()
     {
         x = 42;
     }

     void printx()
     {
         printf("%d\n", x);
     }

Output:

     0
     42
     42
Sep 24 2013
next sibling parent reply "growler" <growlercab gmail.com> writes:
On Wednesday, 25 September 2013 at 04:13:02 UTC, Luís Marques 
wrote:
 Have you seen this one before? Do you know a workaround? (DMD  
 v2.063.2, on OX X 10.9)

 file.d:

     extern(C)
     {
         int x = 0;
         void setx();
         void printx();
     }

     void main()
     {
         setx(); // sets x = 42
         writeln(x); // prints x = 0
         printx(); // prints x = 42
         x = 7;
         printx(); // prints x = 42
     }



 file.c:

     #include <stdio.h>

     extern int x;

     void setx()
     {
         x = 42;
     }

     void printx()
     {
         printf("%d\n", x);
     }

 Output:

     0
     42
     42
don't you need extern __gshared on the var? extern __gshared int x;
Sep 24 2013
parent "growler" <growlercab gmail.com> writes:
On Wednesday, 25 September 2013 at 04:35:35 UTC, growler wrote:
 On Wednesday, 25 September 2013 at 04:13:02 UTC, Luís Marques 
 wrote:
 Have you seen this one before? Do you know a workaround? (DMD  
 v2.063.2, on OX X 10.9)

 file.d:

    extern(C)
    {
        int x = 0;
        void setx();
        void printx();
    }

    void main()
    {
        setx(); // sets x = 42
        writeln(x); // prints x = 0
        printx(); // prints x = 42
        x = 7;
        printx(); // prints x = 42
    }



 file.c:

    #include <stdio.h>

    extern int x;

    void setx()
    {
        x = 42;
    }

    void printx()
    {
        printf("%d\n", x);
    }

 Output:

    0
    42
    42
don't you need extern __gshared on the var? extern __gshared int x;
http://dlang.org/interfaceToC.html (see last section at bottom of page)
Sep 24 2013
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 9/24/2013 9:13 PM, "Luís Marques" <luis luismarques.eu>" wrote:
 Have you seen this one before? Do you know a workaround? (DMD v2.063.2, on OX X
 10.9)

 file.d:

      extern(C)
      {
          int x = 0;
This x is put in thread local storage.
          void setx();
          void printx();
      }

      void main()
      {
          setx(); // sets x = 42
          writeln(x); // prints x = 0
          printx(); // prints x = 42
          x = 7;
          printx(); // prints x = 42
      }



 file.c:

      #include <stdio.h>

      extern int x;
This x is put in thread global storage.
      void setx()
      {
          x = 42;
      }

      void printx()
      {
          printf("%d\n", x);
      }

 Output:

      0
      42
      42
So you've got two different x's here. Declare the first one as: __gshared int x = 0;
Sep 24 2013
parent reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
Oops :-)

I just assumed too quickly that it was another consequence of the 
DMD / (GCC, Clang) section mismatch I reported some days ago 
(http://forum.dlang.org/thread/gqxtzdbhnzkslnltqrmo forum.dlang.org), 
I didn't even think it was a basic PEBCAK.

My bad --;;

I guess I also implicitly assumed extern(C) implied __gshared... 
("make it the same as C, not just the mangling"). And TLS is so 
implicit in D2 that one can even forget about it :-)
Sep 24 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-09-25 06:49, "Luís Marques" <luis luismarques.eu>" wrote:

 I guess I also implicitly assumed extern(C) implied __gshared... ("make
 it the same as C, not just the mangling"). And TLS is so implicit in D2
 that one can even forget about it :-)
Well, C has thread local, via the extension "__thread" or via C++ "thread_local". So if extern (C) implied "__gshared", how would one declare an extern (C) thread local variable? -- /Jacob Carlborg
Sep 25 2013
parent Iain Buclaw <ibuclaw ubuntu.com> writes:
On 25 September 2013 10:40, Jacob Carlborg <doob me.com> wrote:
 On 2013-09-25 06:49, "Lu=EDs Marques" <luis luismarques.eu>" wrote:

 I guess I also implicitly assumed extern(C) implied __gshared... ("make
 it the same as C, not just the mangling"). And TLS is so implicit in D2
 that one can even forget about it :-)
Well, C has thread local, via the extension "__thread" or via C++ "thread_local". So if extern (C) implied "__gshared", how would one decla=
re
 an extern (C) thread local variable?

 --
 /Jacob Carlborg
Especially considering that __thread is deprecated/error in D now. ;) --=20 Iain Buclaw *(p < e ? p++ : p) =3D (c & 0x0f) + '0';
Sep 25 2013