Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop a library in D

Tags:

windows

d

dmd

How would you develop a library in D language?

I want to write a simple library for image processing that I then want to use in an application.

I am looking for analogy to either Java's JARs with Maven system (build, install, use in other projects) or any other package management tool.

What I'd like to know is

  • how to setup a project (two actually, the lib and the app - they are 2 totaly separate projects)
  • how to build, install, share the library
  • are there any rules of thumb, particular visibility of symbols, namespacing conventions etc.

I'm asking this because I don't have the intuition I do in Java or C++.

  • In Java you compile a lib to a JAR and you're good to go. Send it, share it, then just include on claspath and you can reuse it.
  • In C++ you compile it and provide a header file. (or compile it and link to it dynamically)

So what's the story with D?

I am using Visual-D to develop the code, but I have DUB installed as well.

like image 919
Binders Lachel Avatar asked May 26 '14 18:05

Binders Lachel


2 Answers

The generally accepted way is to use dub, a package manager for D.

There is a good collection of dub packages available already: http://code.dlang.org/

Another way would be to simply publish your package as a git repository, then use it as a git submodule. This is the approach I've been using for my libraries.

like image 188
Vladimir Panteleev Avatar answered Sep 27 '22 23:09

Vladimir Panteleev


You can do this with dub by setting target type but i will show another way.

By using MakefileForD,

Why ?

Because dub install lib and bin into ~/.dub . And is not possible to install to a shared dir. As example Linux Filesystem Hierarchy Standard tell that binaries should to go to /usr/bin .

You can't respect this standard by using dub.

Shared lib with Makefile ,

Create a project

myproject
└── src
    └── myproject

Install Makefile_lib into root directory and rename it to Makefile.

Install command.make into root directory

You have now

myproject
├── command.make
├── Makefile
└── src
    └── myproject
        └── main.d

Set source dir

At 5th line from Makefile file

export REPO_SRC_DIR = src

Build

all you have to do now is:

make DC=dmd shared-lib

DC accept dmd ldc and gdc compiler

Install

make install

setting custom install directory

make install PREFIX=/usr LIB_DIR=/usr/lib64

For binaries that is same only instead to take Makefile_lib you need to take Makefile_exe

like image 20
bioinfornatics Avatar answered Sep 27 '22 23:09

bioinfornatics