How to develop a library in D
Asked Answered
J

2

7

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.

Judkins answered 26/5, 2014 at 18:11 Comment(0)
T
4

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.

Tambourin answered 26/5, 2014 at 20:18 Comment(4)
As i said dub is good for personal usage but when you want to distribute and package it you can not use it. How to add your new tool into an official linux distro with dub ? it seem at his time is not possible as dub do not follow FHS.Hypothesis
Thanks. I can get along with dub, but could you please elaborate on the project setup? E.g. how to prepare a project, should I create a binary + headers, or is the binary alone enough? Any articles online you'd recommend?Judkins
I think the best place to start with dub is this page. Follow the links for more information.Tambourin
It still do not answer the question because it doesnt explain what you need to link the "app" project with your library!. It needs the full library source code? exists some like headers in C++? or compiler can resolve symbols from the library file? is the same in LDC, GDC and DMD?Meed
H
2

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

Hypothesis answered 26/5, 2014 at 19:9 Comment(5)
should works on windows. In fact for windows all depends how is gnu makefile support is.Hypothesis
so I need to have make available to cmd.exe ? If I make the library, do I need to distribute some headers too?Judkins
to have "make" into cmd.exe you need to edit the var %PATH% . You need to distribute header for developers which will call your libraryHypothesis
It still do not answer the question because it doesnt explain what you need to link the "app" project with your library!. It needs the full library source code? exists some like headers in C++? or compiler can resolve symbols from the library file? is the same in LDC, GDC and DMD?Meed
D as .di files wich are equivalent to .h from C++. You can to provides .d or .di to link and build your project. Symbol are not cross compiler compatibleHypothesis

© 2022 - 2024 — McMap. All rights reserved.