How to write a library

Steven Schveighoffer schveiguy at gmail.com
Sat Jan 21 23:37:03 UTC 2023


On 1/21/23 5:53 PM, Matt wrote:
> I am trying to write a graphics engine for my university capstone 
> project, and really wanted to give it a try in D, as both a talking 
> point, and because I love the language. I'm using dub to build the 
> library, and the demo application that'll use it.
> 
> However, I've come across a problem. In C/C++, when you build a library, 
> you compile and link the source, then provide the header files for the 
> library user to include. I have built the library, but what is the D 
> equivalent to header files, and what do I have to do to prepare and use 
> my library in another project?

D uses modules, not header files. So no header files are *necessary*.

However, there is one case where you might want to use import files (D's 
equivalent to header files), and that's if you want to declare a 
specific API without providing the implementation.

For a project such as a university project, I would say this isn't 
necessary, as you don't need to obscure the implementation source from 
the users of your library.

If you do want to look into import files, for D, they are `.di` files, 
and should include prototypes for all the public functions you are 
providing, and definitions for all the types. Templates need to be 
included fully implemented source code, not just prototypes (just like C++).

-Steve


More information about the Digitalmars-d-learn mailing list