Hiding implementations

Derek Parnell derek at nomail.afraid.org
Mon Oct 16 21:12:20 PDT 2006


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.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
17/10/2006 2:05:40 PM



More information about the Digitalmars-d-learn mailing list