__init unresolved external when using C library structs converted with dstep
Steven Schveighoffer
schveiguy at gmail.com
Tue Apr 14 18:44:55 UTC 2020
On 4/14/20 2:29 PM, Robert M. Münch wrote:
> On 2020-04-14 18:23:05 +0000, Steven Schveighoffer said:
>
>> On 4/14/20 1:51 PM, Robert M. Münch wrote:
>>> I use a C libary and created D imports with dstep. It translates the
>>> C structs to D structs.
>>>
>>> When I now use them, everything compiles fine but I get an unresolved
>>> external error:
>>>
>>> WindowsApp1.obj : error LNK2019: unresolved external symbol
>>> "myCstruct.__init" (_D7myCStruct6__initZ) referenced in function _Dmain
>>>
>>> Any idea what this is about and how to fix it? I'm wondering why D
>>> tries to find the __init function in the C library and not compile it
>>> from the import.
>>>
>>
>> __init is not a function, it's a static member variable. It's the
>> `intializer` property inside TypeInfo used for initialization.
>
> Ah, ok. That's why the problem went (temporarly) away when I did a:
> myCstruct a = {0,0}; for example?
I don't know what causes it to be emitted when. Sometimes it doesn't
make a whole lot of sense to me.
>
>> Did you extern(C) the struct?
>
> Yes, everything is "extern(C) :" for the complete import files.
Then apparently the compiler still expects to have initializers even for
foreign language structs. This is not a huge issue though, as the
structs are ABI compatible even if compiled in D.
>
>> Are you compiling the D file that contains the struct definition as well?
>
> No. Is that the missing part?
>
Probably. I think the compiler expects whatever is compiling the
imported file to provide the symbol. If you aren't compiling it
separately, then you need to include it in the compilation.
-Steve
More information about the Digitalmars-d-learn
mailing list