digitalmars.D - Initialized global C var
- Bob W (23/23) Apr 13 2005 Seems that initialized global C variables
- brad domain.invalid (4/42) Apr 13 2005 I'm guessing that D is creating the extern (C) int b variable itself?
- Carlos Santander B. (5/43) Apr 13 2005 Put your extern(C) declarations in another file but do not link it.
- Bob W (25/42) Apr 14 2005 Maybe I should add some more info:
- Walter (15/19) Apr 15 2005 Create the following D code, let's call it externa.d, with these content...
Seems that initialized global C variables cannot be accessed from D, but neither compiler nor linker are complaining about attempting it. Is this correct or did I just miss out something? Example: // C code: extern int a; static int b=1234; // global (local to file ...) int pass(int x) { b+=x; a=b; return(b); } // D code: import std.stdio; extern (C) int a; extern (C) int b; extern (C) int pass(int x); void main() { writefln("'pass' returned: %d",pass(1)); // this works writefln("Value of a: %d",a); // this is ok writefln("Value of b: %d",b); // but this one not }
Apr 13 2005
Bob W wrote:Seems that initialized global C variables cannot be accessed from D, but neither compiler nor linker are complaining about attempting it. Is this correct or did I just miss out something? Example: // C code: extern int a; static int b=1234; // global (local to file ...) int pass(int x) { b+=x; a=b; return(b); } // D code: import std.stdio; extern (C) int a; extern (C) int b; extern (C) int pass(int x); void main() { writefln("'pass' returned: %d",pass(1)); // this works writefln("Value of a: %d",a); // this is ok writefln("Value of b: %d",b); // but this one not }I'm guessing that D is creating the extern (C) int b variable itself? Ie, I imagine that your C code could access D's external int b. Brad
Apr 13 2005
Bob W wrote:Seems that initialized global C variables cannot be accessed from D, but neither compiler nor linker are complaining about attempting it. Is this correct or did I just miss out something? Example: // C code: extern int a; static int b=1234; // global (local to file ...) int pass(int x) { b+=x; a=b; return(b); } // D code: import std.stdio; extern (C) int a; extern (C) int b; extern (C) int pass(int x); void main() { writefln("'pass' returned: %d",pass(1)); // this works writefln("Value of a: %d",a); // this is ok writefln("Value of b: %d",b); // but this one not }Put your extern(C) declarations in another file but do not link it. -- Carlos Santander Bernal JP2, you'll always live in our minds
Apr 13 2005
Maybe I should add some more info: The C code will be compiled (but not linked) to a D compatible object file (OMF).// C code: extern int a; static int b=1234; // global (local to file ...) int pass(int x) { b+=x; a=b; return(b); }The D code will be compiled and linked with the object file derived form the C code.// D code: import std.stdio; extern (C) int a; extern (C) int b; extern (C) int pass(int x); void main() { writefln("'pass' returned: %d",pass(1)); // this works writefln("Value of a: %d",a); // this is ok writefln("Value of b: %d",b); // but this one not }Results: - 'a' will be generated by the linker and can be accessed by the C and the D code, but of course it cannot be automatically initialized. - 'pass()' proves that 'b' is properly initialized and globally available to the C code. - 'b' cannot be accessed by the D code, but the linker does not complain about this. When attempting to get the value of 'b' from the D code, it will always be 0. Why does that bother me? I would like to access some functions (no problem) AND the initialized data of a C program, which I won't rewrite to D, because it would take too long. Furthermore I am not keen writing interface functions which make the data available to D if there is a simpler way to do it. If the D code is replaced by a C program, the whole thing works as expected. So I guess there might be a way to teach D to do the trick. Any ideas?
Apr 14 2005
"Bob W" <nospam aol.com> wrote in message news:d3lh01$l0f$1 digitaldaemon.com...If the D code is replaced by a C program, the whole thing works as expected. So I guess there might be a way to teach D to do the trick. Any ideas?Create the following D code, let's call it externa.d, with these contents: extern (C) int a; Then, use it as: import std.stdio; import externa; // extern (C) int a; extern (C) int b; extern (C) int pass(int x); void main() { writefln("'pass' returned: %d",pass(1)); // this works writefln("Value of a: %d",a); // this is ok writefln("Value of b: %d",b); // but this one not } Compile and link, but do not link in externa.obj.
Apr 15 2005