digitalmars.D.learn - Interfacing to C. extern (C) variables
- Alex_Dovhal (38/38) Jun 26 2011 Hi. I try to interface D module with C one. See:
- Robert Clipsham (5/7) Jun 26 2011 extern(C) extern int myCVar;
- Alex_Dovhal (63/67) Jun 26 2011 Thanks for answer. I've already tried that, it doesn't work for some rea...
- Jimmy Cao (6/75) Jun 26 2011 It works for me like this:
- Alex_Dovhal (2/7) Jun 26 2011 Thanks.
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
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
"Robert Clipsham" <robert octarineparrot.com> wrote:On 26/06/2011 20:54, Alex_Dovhal wrote: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(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); }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;
Jun 26 2011
On Sun, Jun 26, 2011 at 5:00 PM, Alex_Dovhal <alex_dovhal yahoo.com> wrote:"Robert Clipsham" <robert octarineparrot.com> wrote:It works for me like this: extern(C){ __gshared int c_var; int func(); }On 26/06/2011 20:54, Alex_Dovhal wrote: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(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); }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;
Jun 26 2011
"Jimmy Cao" <jcao219 gmail.com> wrote:It works for me like this: extern(C){ __gshared int c_var; int func(); }Thanks.
Jun 26 2011