Importing modules under DUB on Windows

Mike Parker aldacron at gmail.com
Fri Oct 28 02:20:23 UTC 2022


On Thursday, 27 October 2022 at 16:40:20 UTC, DLearner wrote:

>
> I'm not getting on with DUB.
> Maybe fewer people use it under Windows, so Windows constructs 
> don't get exercised so much.
>
> Is there a non-DUB way of arranging that
> `import arsd.terminal;`
> will use that module as held on GitHub?
> (DUB name: "arsd-official:terminal": "~>10.9.4").
>
> OS: Windows 10. Compiler: DMD.
>

This has nothing to do with Windows. The source path error you 
encountered is an assertion failure that tells you what's wrong: 
dub does not allow you to add absolute paths to the source path. 
I don't know why. If you disagree, then you can file an issue in 
the dub github repository.

In the meantime, you'll need to work around the limitation. You 
could

1. Add the file to your source tree
2. Use a relative path in `sourcePaths`
2. Add arsd-official as a dub dependency

All of these would solve the problem. If you aren't going to do 
any of these things, then you'll need a fourth option.

The compiler (not dub) needs to know where to find imports, and 
the linker needs an object file to resolve symbols. So you should:

1. Compile /arsd/terminal.d as a static library with ldc (named 
e.g., "arsd-terminal.lib") and output the library to the path 
where you want it (e.g., C:\libs)
2. Add the parent directory of the arsd folder to your 
`importPaths` in dub.
3. Add linker flags to your dub project to tell the compiler 
where to find the library

You have two options for #3. If this is the only external libs 
you're working with, then you can just add the full path to the 
library in an `libs` entry:

```json
"libs": ["C:\\libs\\arsd-terminal"]
```

Note that you do not add the ".lib" extension here. Dub will do 
that for you.

If you have multiple external libs, then it's better to add the 
library path like so:

```json
"lflags": ["/LIBPATH:C:\\libs"],
"libs": ["lib1", "lib2", "lib3"]
```

Note that `/LIBPATH` is specific to the Microsoft linker, which I 
assume LDC uses on Windows:

https://learn.microsoft.com/en-us/cpp/build/reference/libpath-additional-libpath?view=msvc-170

If it's using the LLVM linker, then you'll need the appropriate 
flag for that.

If you aren't planning to build on other platforms, then you'll 
want to make these dub directives platform specific, e.g., 
`libs-windows` rather than `libs`.



More information about the Digitalmars-d-learn mailing list