Search code examples
d

How do you compile multiple files in different folders in D?


project
---|source
------ |controllers
-------|models
-------|lib
----------|field.d
-------|app.d

I run dub but I get this error:

Error: module field from file ... conflicts with another module field from file source/lib/field.d

field.d looks like this:

   module field;

    class Field(T){

        this(T def_val,bool required,string help_text);

        bool validate();
        private bool _validate(); 
    }

Solution

  • Always put a module statement in any file that is going to be imported, and use a package name consistently as well to avoid conflicts.

    So, instead of calling it simply module field;, call it module myapplication.field;, or even module myapplication.lib.field;, and of course, also import it by the same full name when you use it.

    I'm not sure if dub will just work like that though (I don't use it personally), but the language lets you give a module any name, even if it doesn't match the filename, which helps in situations like this, avoiding name conflicts.

    In general, if you give them all full, unique names, then compile them all at once: dmd app.d lib/field.d [and any other files your project has], things will just work.