www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Interfacing to C. extern (C) variables

reply "Alex_Dovhal" <alex_dovhal yahoo.com> writes:
Hi. I try to interface D module with C one. See:

* file test.c:
#include <stdio.h>
int c_var;
int func()
{
    c_var = 1234;
    printf("In C\n");
    printf("c_var = %d\n", c_var);
    printf("&c_var = %x\n", &c_var);
    return 0;
}

* file unit.d:
module unit;
import std.stdio;
extern(C){
    int c_var;
    int func();
}

void main()
{
    func();
    writeln("In D");
    writefln("c_var = %d", c_var);
    writefln("&c_var = %x", &c_var);
}

And compile:
    dmc -c test.c
    dmd unit.d test.obj

I'd like to call C functions and use C variables in D module.
D calls C functions just fine, but how can I use C variables?

This program outputs:
In C
c_var = 1234
&c_var = 45d004
In D
c_var = 0
&c_var = 14298c
Jun 26 2011
parent reply Robert Clipsham <robert octarineparrot.com> writes:
On 26/06/2011 20:54, Alex_Dovhal wrote:
 I'd like to call C functions and use C variables in D module.
 D calls C functions just fine, but how can I use C variables?

extern(C) extern int myCVar; -- Robert http://octarineparrot.com/
Jun 26 2011
next sibling parent "Alex_Dovhal" <alex_dovhal yahoo.com> writes:
"Robert Clipsham" <robert octarineparrot.com> wrote:
 On 26/06/2011 20:54, Alex_Dovhal wrote:
 I'd like to call C functions and use C variables in D module.
 D calls C functions just fine, but how can I use C variables?

extern(C) extern int myCVar;

Thanks for answer. I've already tried that, it doesn't work for some reason: * file unit2.d: module unit; import std.stdio; extern(C) int func(); extern(C) extern int c_var; void main() { func(); writeln("In D"); writefln("c_var = %d", c_var); writefln("&c_var = %x", &c_var); } Result: In C c_var = 1234 &c_var = 470200 In D object.Error: Access Violation ---------------- 426DA4 426C1B 405BEB 4057E7 451099 ---------------- Found workaround, but it's extreme hack: *file test.c #include <stdio.h> int c_var; //int* ref_c_var() //{ // return &c_var; //} #define REF_DECL(type, name) type* ref_ ## name () {return &name;} REF_DECL(int, c_var) int func() { c_var = 1234; printf("In C\n"); printf("c_var = %d\n", c_var); printf("&c_var = %x\n", &c_var); return 0; } *file: unit1.d: module unit; import std.stdio; extern(C) int func(); string C_VAR(string type, string name) { return "extern (C) "~ type ~ "* ref_" ~ name ~ "();"~ " " ~ "ref " ~ type ~ " " ~ name ~"(){return *ref_"~name~"();}"; } mixin(C_VAR("int", "c_var")); void main() { func(); writeln("In D"); writefln("c_var = %d", c_var); writefln("&c_var = %x", &c_var); }
Jun 26 2011
prev sibling parent reply Jimmy Cao <jcao219 gmail.com> writes:
--0016363b8cecb9ba4804a6a5819e
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Jun 26, 2011 at 5:00 PM, Alex_Dovhal <alex_dovhal yahoo.com> wrote:

 "Robert Clipsham" <robert octarineparrot.com> wrote:
 On 26/06/2011 20:54, Alex_Dovhal wrote:
 I'd like to call C functions and use C variables in D module.
 D calls C functions just fine, but how can I use C variables?

extern(C) extern int myCVar;

Thanks for answer. I've already tried that, it doesn't work for some reason: * file unit2.d: module unit; import std.stdio; extern(C) int func(); extern(C) extern int c_var; void main() { func(); writeln("In D"); writefln("c_var = %d", c_var); writefln("&c_var = %x", &c_var); } Result: In C c_var = 1234 &c_var = 470200 In D object.Error: Access Violation ---------------- 426DA4 426C1B 405BEB 4057E7 451099 ---------------- Found workaround, but it's extreme hack: *file test.c #include <stdio.h> int c_var; //int* ref_c_var() //{ // return &c_var; //} #define REF_DECL(type, name) type* ref_ ## name () {return &name;} REF_DECL(int, c_var) int func() { c_var = 1234; printf("In C\n"); printf("c_var = %d\n", c_var); printf("&c_var = %x\n", &c_var); return 0; } *file: unit1.d: module unit; import std.stdio; extern(C) int func(); string C_VAR(string type, string name) { return "extern (C) "~ type ~ "* ref_" ~ name ~ "();"~ " " ~ "ref " ~ type ~ " " ~ name ~"(){return *ref_"~name~"();}"; } mixin(C_VAR("int", "c_var")); void main() { func(); writeln("In D"); writefln("c_var = %d", c_var); writefln("&c_var = %x", &c_var); }

It works for me like this: extern(C){ __gshared int c_var; int func(); } --0016363b8cecb9ba4804a6a5819e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Sun, Jun 26, 2011 at 5:00 PM, Alex_Dovhal <span dir=3D"ltr">&lt;<a href= =3D"mailto:alex_dovhal yahoo.com">alex_dovhal yahoo.com</a>&gt;</span> wrot= e:<br><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D= "margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div class=3D"im">&quot;Robert Clipsham&quot; &lt;<a href=3D"mailto:robert = octarineparrot.com">robert octarineparrot.com</a>&gt; wrote:<br> &gt; On 26/06/2011 20:54, Alex_Dovhal wrote:<br> &gt;&gt; I&#39;d like to call C functions and use C variables in D module.<= br> &gt;&gt; D calls C functions just fine, but how can I use C variables?<br> &gt;<br> &gt; extern(C) extern int myCVar;<br> <br> </div>Thanks for answer. I&#39;ve already tried that, it doesn&#39;t work f= or some reason:<br> <br> * file unit2.d:<br> <div class=3D"im">module unit;<br> import std.stdio;<br> </div>extern(C) int func();<br> <div class=3D"im">extern(C) extern int c_var;<br> <br> </div><div class=3D"im">void main()<br> {<br> =A0 =A0func();<br> =A0 =A0writeln(&quot;In D&quot;);<br> =A0 =A0writefln(&quot;c_var =3D %d&quot;, c_var);<br> =A0 =A0writefln(&quot;&amp;c_var =3D %x&quot;, &amp;c_var);<br> }<br> <br> </div>Result:<br> <div class=3D"im">In C<br> c_var =3D 1234<br> </div>&amp;c_var =3D 470200<br> In D<br> object.Error: Access Violation<br> ----------------<br> 426DA4<br> 426C1B<br> 405BEB<br> 4057E7<br> 451099<br> ----------------<br> <br> <br> Found workaround, but it&#39;s extreme hack:<br> <br> *file test.c<br> <div class=3D"im">#include &lt;stdio.h&gt;<br> int c_var;<br> </div>//int* ref_c_var()<br> //{<br> // =A0 =A0return &amp;c_var;<br> //}<br> <br> #define REF_DECL(type, name) type* ref_ ## name () {return &amp;name;}<br> <br> REF_DECL(int, c_var)<br> <div class=3D"im"><br> int func()<br> {<br> =A0 =A0c_var =3D 1234;<br> =A0 =A0printf(&quot;In C\n&quot;);<br> =A0 =A0printf(&quot;c_var =3D %d\n&quot;, c_var);<br> =A0 =A0printf(&quot;&amp;c_var =3D %x\n&quot;, &amp;c_var);<br> =A0 =A0return 0;<br> }<br> <br> </div>*file: unit1.d:<br> <div class=3D"im">module unit;<br> import std.stdio;<br> <br> </div>extern(C) int func();<br> <br> string C_VAR(string type, string name)<br> {<br> =A0 =A0return<br> =A0 =A0 =A0 =A0&quot;extern (C) &quot;~ type ~ &quot;* ref_&quot; ~ name ~= &quot;();&quot;~<br> =A0 =A0 =A0 =A0&quot; &quot; ~ &quot;ref &quot; ~ type ~ &quot; &quot; ~ n= ame ~&quot;(){return *ref_&quot;~name~&quot;();}&quot;;<br> }<br> <br> mixin(C_VAR(&quot;int&quot;, &quot;c_var&quot;));<br> <div><div></div><div class=3D"h5"><br> void main()<br> {<br> =A0 =A0func();<br> =A0 =A0writeln(&quot;In D&quot;);<br> =A0 =A0writefln(&quot;c_var =3D %d&quot;, c_var);<br> =A0 =A0writefln(&quot;&amp;c_var =3D %x&quot;, &amp;c_var);<br> }<br> <br> <br> </div></div></blockquote></div><br><div><br></div><div>It works for me like= this:</div><div><br></div><blockquote class=3D"webkit-indent-blockquote" s= tyle=3D"margin: 0 0 0 40px; border: none; padding: 0px;"><div><div>extern(C= ){</div> <div>=A0 =A0__gshared int c_var;</div><div>=A0 =A0int func();</div><div>}</= div></div></blockquote> --0016363b8cecb9ba4804a6a5819e--
Jun 26 2011
parent "Alex_Dovhal" <alex_dovhal yahoo.com> writes:
"Jimmy Cao" <jcao219 gmail.com> wrote:
It works for me like this:

extern(C){
   __gshared int c_var;
   int func();
}

Thanks.
Jun 26 2011