www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - referencing variables declared in an external c file

reply llee <llee goucher.edu> writes:
     I'm writing an application that is compiled against several object files
that have been coded in c. These files define a variable named yyin that has
the following prototype: FILE* yyin.
     I need to access this variable from my d code. I tried referencing the
variable using the following statement: yyin = fopen (&fname [0], &fmode [0]);,
but the compiler returned an error stating that yyin was undefined. I tried
including the following extern (C) statement: extern (C) FILE* yyin;, but the
linker complained that yyin was defined multiple times.\
Jul 02 2008
parent reply BCS <ao pathlink.com> writes:
Reply to llee,

 I'm writing an application that is compiled against several
 object files that have been coded in c. These files define a variable
 named yyin that has the following prototype: FILE* yyin.
 I need to access this variable from my d code. I tried
 referencing the variable using the following statement: yyin = fopen
 (&fname [0], &fmode [0]);, but the compiler returned an error stating
 that yyin was undefined. I tried including the following extern (C)
 statement: extern (C) FILE* yyin;, but the linker complained that yyin
 was defined multiple times.\
put "extern(C) FILE* yyin;" in another d file and import it but don't link it in. I think that will work somefile.c extern FILE* yyin; header.d: extern(C) FILE* yyin; use.d import header; void main(){writef("%x\n", cast(void*)yyin);} gcc -c somefile.c; dmd -c use.d; dmd use.o somefile.o
Jul 02 2008
parent reply llee <llee goucher.edu> writes:
BCS Wrote:

 Reply to llee,
 
 I'm writing an application that is compiled against several
 object files that have been coded in c. These files define a variable
 named yyin that has the following prototype: FILE* yyin.
 I need to access this variable from my d code. I tried
 referencing the variable using the following statement: yyin = fopen
 (&fname [0], &fmode [0]);, but the compiler returned an error stating
 that yyin was undefined. I tried including the following extern (C)
 statement: extern (C) FILE* yyin;, but the linker complained that yyin
 was defined multiple times.\
put "extern(C) FILE* yyin;" in another d file and import it but don't link it in. I think that will work somefile.c extern FILE* yyin; header.d: extern(C) FILE* yyin; use.d import header; void main(){writef("%x\n", cast(void*)yyin);} gcc -c somefile.c; dmd -c use.d; dmd use.o somefile.o
it works.
Jul 02 2008
parent Frank Benoit <keinfarbton googlemail.com> writes:
llee schrieb:
 BCS Wrote:
 somefile.c
 extern FILE* yyin;

 header.d:
 extern(C) FILE* yyin;

 use.d
 import header;
 void main(){writef("%x\n", cast(void*)yyin);}


 gcc -c somefile.c;
 dmd -c use.d;
 dmd  use.o somefile.o
it works.
you can link the file if you use extern extern(C) FILE* yyin; a dangerous pitfall to forget the first extern.
Jul 02 2008