Hiding implementations

Bill Baxter dnewsgroup at billbaxter.com
Mon Oct 16 21:35:45 PDT 2006


Excellent. Thanks.
Sometimes it's nice to have header files around just as a quick 
reference to what's in a big API.  Nice that they can be generated 
automatically.

--bb

Derek Parnell wrote:
> On Tue, 17 Oct 2006 11:48:23 +0900, Bill Baxter wrote:
> 
>> Since D has no header files, is it possible to distribute binary-only 
>> libraries?  With C++ you can ship just header files and compiled libs, 
>> then the users can see the interface but not the implementation.  How 
>> can you do that with D?
>>
>> --bb
> 
> D does have 'header' files. By convention they have the suffix ".di" and
> only contain the interface stuff.
> 
> For example:
> Implementation file -- mymod.d
> // ------ start of file ----------
> private import std.stdio;
> void myfunc(int x)
> {
>     std.stdio.writefln("The ANSWER is %s", x);
> }
> // --------- end of file ----------
> 
> Interface file -- mymod.di
> // ------ start of file ----------
> private import std.stdio;
> void myfunc(int x);
> // --------- end of file ----------
> 
> 
> You compile the implementation file and supply either the object file or a
> library containing the object file. You then use it as ...
> 
>  //--- example 
>  import mymod;
>  void main() { mymod.myfunc(42); }
>  // --------- end of file
> 
> 
>  dmd example.d mymod.di thelibrary.lib 
> 
> You can also generate the header file by doing ...
> 
>   dmd myfunc.d -H
> 
> This will create the myfunc.di file for you.
> 



More information about the Digitalmars-d-learn mailing list