How to create a library in D

How do you create a library in D?

I want to write a simple library for image processing, which I then want to use in the application.

I am looking for an analogy with the Java JAR with the Maven system (assembly, installation, use in other projects) or any other package management tool.

What I would like to know

  • how to set up a project (two actually, lib and application are 2 completely separate projects)
  • how to create, install, share a library
  • are there any rules of thumb, specific visibility of characters, name conventions, etc.

I ask about this because I do not have the intuition that I do in Java or C ++.

  • In Java, you compile lib in a JAR and you are good to go. Submit it, share it, then just include it in 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 is the story with D?

I use Visual-D for code development, but I also have DUB installed.

+6
source share
2 answers

A common way is to use dub, the package manager for D.

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

Another way would be to simply publish your package as a git repository, and then use it as a submodule of git. This is the approach I used for my libraries.

+3
source

You can do this with dub by setting target , but I will show it in a different way.

Using MakefileForD ,

Why?

Because dub sets lib and bin to ~/.dub . And it is impossible to install in a shared directory. As an example, the Linux File System Hierarchy Standard reports that binaries should go to /usr/bin .

You cannot respect this standard using dub.

Generic lib with Makefile,

Create project

 myproject └── src └── myproject 

Set Makefile_lib to the root directory and rename it to Makefile.

Install command.make in the root directory

You have

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

Set source directory

In the 5th line from the Makefile

 export REPO_SRC_DIR = src 

Build

all you have to do is:

 make DC=dmd shared-lib 

DC accepts dmd ldc and gdc compiler

Install

 make install 

setting up a custom installation directory

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

For binaries that are the same, but need to do Makefile_lib instead, you need to do Makefile_exe

+1
source

Source: https://habr.com/ru/post/969872/


All Articles