9

I´m new to the D Programming Language and have a very simple problem.

I want to compile a D Script Library once and then use it in my other D projects.

In C I linked to the .lib files and created headers for them, but in D I don´t find things like that (are there even some sort of headers in D?)

I use D-IDE as my IDE and DMD2 as my compiler.

sth
  • 222,467
  • 53
  • 283
  • 367
Moritz Schöfl
  • 763
  • 2
  • 7
  • 19

2 Answers2

11

Create StaticLib.d:

module StaticLib;

int func(int x)
{
    return x+1;
}

Compile it:

dmd -lib StaticLib.d -ofStaticLib.lib

Create App.d:

module App;
import std.stdio;
import StaticLib;

void main(string[] args)
{
    writeln("func(3) = ", StaticLib.func(3));
}

Create StaticLib.di (d header):

int func(int x);

Compile it:

dmd App.d StaticLib.di StaticLib.lib -ofApp.exe
Max Alibaev
  • 681
  • 7
  • 17
dnewbie
  • 111
  • 2
  • [Language spec](http://dlang.org/spec/module.html#module_declaration): > By convention, package and module names are all lower case. This is because those names can have a one-to-one correspondence with the operating system's directory and file names, and many file systems are not case sensitive. All lower case package and module names will minimize problems moving projects between dissimilar file systems. – sigod Apr 16 '16 at 07:39
7

there are .di (D interface) files which can be used as header these can be generated from your sources with the -H compiler switch

however the libraries I've seen will just have the source files to import

you can use the -I switch to specify where the compiler will look for imports

and the -L switch will be passed to the linker

ratchet freak
  • 47,288
  • 5
  • 68
  • 106