| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
July 02, 2008 referencing variables declared in an external c file | ||||
|---|---|---|---|---|
| ||||
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.\
| ||||
July 02, 2008 Re: referencing variables declared in an external c file | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | 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
| |||
July 02, 2008 Re: referencing variables declared in an external c file | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | 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.
| |||
July 02, 2008 Re: referencing variables declared in an external c file | ||||
|---|---|---|---|---|
| ||||
Posted in reply to llee | 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply