referencing variables declared in an external c file

llee llee at goucher.edu
Wed Jul 2 13:08:40 PDT 2008


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. 




More information about the Digitalmars-d mailing list