digitalmars.D - referencing variables declared in an external c file
- llee (2/2) Jul 02 2008 I'm writing an application that is compiled against several object ...
- BCS (13/22) Jul 02 2008 put "extern(C) FILE* yyin;" in another d file and import it but don't li...
- llee (2/34) Jul 02 2008 it works.
- Frank Benoit (4/23) Jul 02 2008 you can link the file if you use
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
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
BCS Wrote:Reply to llee,it works.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
llee schrieb:BCS Wrote:you can link the file if you use extern extern(C) FILE* yyin; a dangerous pitfall to forget the first extern.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.oit works.
Jul 02 2008