dub: how to reference a compiled package

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 25 18:49:20 PST 2016


On Thursday, 25 February 2016 at 21:06:59 UTC, mahdi wrote:
> On Thursday, 25 February 2016 at 16:45:46 UTC, Chris Wright

> Thanks. Is there a way to use a D library without having access 
> to it's source code? I tried `dmd -lib abcd.d` which creates a 
> static library. But still I need to specify path to library's 
> source files using -I option when compiling the code that uses 
> that library.
>
> So if we have just access to the library file, is it possible 
> to use it in the code?

The compiler needs to know what symbols are available from any 
imports you use in your source. .di files exist to allow closed 
source projects to be distributed as binary. They are analagous 
to C or C++ header files. You could create them by hand like so:

// foo.d
struct S { int x, y; }
void addTwo(S s) { s.x += 2; s.y += 2; }

// foo.di
struct S { int x, y; }
void addTwo(S s);

The compiler needs to know about S and its types, and it needs to 
know the signature of addTwo. The .di file allows you to provide 
that while keeping the implementation of addTwo closed. When foo 
is imported in client code, the compiler will find foo.di and use 
that instead of foo.d.

However, the compiler must have the source for templates, as they 
are instantiated when they are used, not when the library is 
compiled. The same is true for any functions you want inlined. In 
the example above, addTwo can only be inlined when foo.d is used, 
since the compiler will not have the implementation with foo.di.


More information about the Digitalmars-d-learn mailing list