www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Initialized global C var

reply "Bob W" <nospam aol.com> writes:
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
next sibling parent brad domain.invalid writes:
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
prev sibling next sibling parent "Carlos Santander B." <csantander619 gmail.com> writes:
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
prev sibling parent reply "Bob W" <nospam aol.com> writes:
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
parent "Walter" <newshound digitalmars.com> writes:
"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