'D language import local module
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 ?
Solution 1:[1]
The official documentation for writing and using shared libraries in D on Linux is here:
Solution 2:[2]
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.
Solution 3:[3]
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="../"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Michael Parker |
Solution 3 | Vijay Nayar |