On Monday, 29 February 2016 at 21:43:31 UTC, Sebastien Alaiwan wrote:

Ok so now let's say I rename the directory "lib" to "foo". If I don't change the "import lib.hello" to "import foo.hello", how is the compiler going to find "hello.d"?

You have to tell the compiler where it is.
Is it only possible with separate compilation?


Given two modules, src/lib/foo.d and and src/main.d, you can pass them both to the command line and not worry about passing -I to the compiler.

dmd src/main.d src/lib/foo.d

In this case, foo.d can have a module statement that gives it any name at all. It does not need to match its directory structure:

module wicked.cool.mod;

This works because you've given the module directly to the compiler and it doesn't need to search for it. If you compile them separately:

dmd -c src/lib/foo.d
dmd src/main.d foo.obj

Now you will see a compiler error. You've given the compiler foo's object file, but it doesn't know where the source file is. When main.d imported wicked.foo.mod, the compiler tried to find wicked/foo/mod.d from its current working directory. No joy. You can test this changing the module declaration in foo.d to lib.foo, then importing lib.foo in main.d. The you can do this:

cd src
dmd -c lib/foo.d
dmd main.d foo.obj

Now when it encounters import lib.foo, it will look for lib/foo.d and find it. Now do this:

cd ..
dmd -c src/lib/foo.d
dmd src/main.d foo.obj

Again, failure, because it's looking for lib/foo.d. So now you have to tell it where to find lib/foo.d:

dmd -Isrc src/main.d foo.obj

Now it will look for src/lib/foo.d and find it.

This is why you shouldn't muck about with package names. If you want to change the name of the src directory, that's fine. You can simply pass -I to the compiler with the correct value. But if you want to change the name of lib, you are now affecting the compiler's ability to find imports. This becomes a problem with separate compilation (as above), which also means it's a problem when using libraries. If you change a package name for something you're distributing as a library, then all the import statements in client code will need to be changed as well.



Reply via email to