www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to write a library

reply Matt <webwraith fastmail.fm> writes:
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?
Jan 21 2023
next sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 21 January 2023 at 22:53:19 UTC, Matt wrote:
 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?
The most common and easiest thing in D is to just distribute the source files, the compiler can pull whatever it needs out of there. I almost never even build libraries separately, instead letting the compiler include them as-needed in the build.
Jan 21 2023
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jan 21 2023
prev sibling parent Hipreme <msnmancini hotmail.com> writes:
On Saturday, 21 January 2023 at 22:53:19 UTC, 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?
For using your thing as a library you need to do 2 things: 1: Include the library files by using `importPaths` on dub or -I= on your favorite compiler 2: Add the linker flag to include the library. importPaths don't compile your source files. Only templates and some ctfe features can be used when using importPaths.
Jan 21 2023