Imports
Mike Parker
aldacron at gmail.com
Thu Oct 5 00:28:32 UTC 2017
On Wednesday, 4 October 2017 at 16:31:35 UTC, Jiyan wrote:
> Hey,
>
> as i see it the -Ipath command for dmd just imports the files
> within a directory but it doesnt work for sub directories, so i
> can write something like:
>
> import subdirectoryFromPath.file;
>
> Also with dub this doesnt seem possible (sourcePaths seems to
> work as the -I command).
>
> Is there a way to do what i want? Or am i doing something wrong?
If you have this directory tree:
- mylib
-- pack1
--- a.d
--- b.d
---- pack2
----- c.d
Then you would pass -Imylib to the compiler. In your code, you
can write the following:
import pack1.a;
import pack1.pack2.c;
You don't import files or directories, just packages and modules.
By default, package names correspond to directory names and
module names correspond to file names, but they don't have to
(it's best practice, though).
> And by the way what is the difference from sourcePaths to
> importPaths?
sourcePaths where DUB can find additional files, outside of the
default source directory, to pass to the compiler for
compilation. importPaths will all be passed to the compile as -I.
It seems you think that importing a module causes its source file
to be automatically compiled. That doesn't happen.
imports are strictly for the compiler to know which symbols are
available for the current module to use. It does not attempt to
compile imported modules. Imported modules might be part of your
program or they might be part of a precompiled library. In the
latter case, they don't need to be compiled because they already
are. So the compiler leaves it up to you to decide which modules
need to be compiled.
Dub, by default, will make sure all modules in your source
directory are compiled. It will also guarantee the compiler knows
where to find them for imports. Sometimes, you might also want to
import files from a library that isn't available from
code.dlang.org. You can use importPaths to tell dub additional
paths to give the compiler. If those files are part of a
precompiled library you can link the library and you're done. If
they aren't, you can use sourcePaths to tell DUB that all the
source modules in the additional paths also need to be passed to
the compiler for compiling and linking into the final executable.
More information about the Digitalmars-d-learn
mailing list