D language import local module

316 views Asked by At

I am new to D language. i have a project A. When i executed dub build it has created me libA.so. i am trying to create a different .d file and import this module.

now i wanted to write a D wrapper to consume it.

when i tried

import a

it did not work. i copied the libA.so to the sudo cp libA.so /usr/include/dmd/druntime/import/ but the import did not work.

Can you please help me how can i import this ?

3

There are 3 answers

0
Vladimir Panteleev On

The official documentation for writing and using shared libraries in D on Linux is here:

https://dlang.org/articles/dll-linux.html

0
Michael Parker On

You don't import shared libraries in D. You import modules (source files). So the .so file does not belong in the import directory. You need to link to that. import a means the compiler will search the import path for a.d. That's what needs to be in your import directory. If your library has multiple source files, they each need to be on the import path and imported separately.

0
Vijay Nayar On

The question is a bit unclear, but it sounds like you want to create a library via dub, and then use that library in another project. Is that correct?

For example, your project may have a structure like:

myProject
├- commonLib
|
├- program1
|  (depends on commonLib)
└- program2
   (depends on commonLib)

If this is what you are trying to do, you should consider using Dub sub-packages.

For example, myProject/dub.sdl might look like this:

name "myProject"
targetType "none"
dependency "myProject:common" path="."
dependency "myProject:program1" path="."
dependency "myProject:program2" path="."
subPackage "./common/"
subPackage "./program1/"
subPackage "./program2/"

Your common library at myProject/common/dub.sdl may look like this:

name "common"
targetType "library"

And finally you would depend on it like this in myProject/program1/dub.sdl:

name "program1"
targetType "executable"
dependency "myProject:common" path="../"