Passing opaque struct between functions/modules

Mike Parker aldacron at gmail.com
Thu Jan 24 01:04:08 PST 2013


On Wednesday, 23 January 2013 at 16:33:08 UTC, Sarath Kumar wrote:
> DMD v2.61; openSUSE 12.1
>
> Source:
> -------
> libA.d:
>
> module libA;
>
> extern (C)
> {
>         struct Opaque;
>
>         Opaque* getObject();
>
>         void doSomething(Opaque *);
> }
> ----------
> libB.d:
>
> module libB;
>
> extern (C)
> {
>         struct Opaque;
>
>         void doAction(Opaque *);
> }
> -----------
> bug.d
>
> import libA, libB;
>
> int main(string[] args)
> {
>         auto opaque = libA.getObject();
>         libA.doSomething(opaque); // this is okay
>         libB.doAction(opaque);  // but this is not, compiler 
> error here
>         return 0;
> }
>
> When I compile the above files, I get the below error.
> $ rdmd bug.d
> bug.d(7): Error: function libB.doAction (Opaque*) is not 
> callable using argument types (Opaque*)
> bug.d(7): Error: cannot implicitly convert expression (opaque) 
> of type Opaque* to Opaque*
>
> If I do an explicit cast, libB.Opaque*, for opaque in line 7, 
> bug.d, I get completely different set of errors.
>
> $ rdmd bug.d
> libB.d(5): Error: struct libB.Opaque unknown size
> libB.d(5): Error: struct libB.Opaque no size yet for forward 
> reference
> libB.d(5): Error: struct libB.Opaque unknown size
> libB.d(5): Error: struct libB.Opaque no size yet for forward 
> reference
> libA.d(5): Error: struct libA.Opaque unknown size
> libA.d(5): Error: struct libA.Opaque no size yet for forward 
> reference
>
> Can someone please tell me the right way to pass an opaque 
> object between module's functions.

The error message here is deceiving. Declare the struct in one 
module only and then import it in every module that uses it. So, 
for example, keep the declaration in libA, remove it from libB 
and in libB add "import libA;".


More information about the Digitalmars-d-learn mailing list