www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot find module during separate compilation

reply Satoshi <satoshi gshost.eu> writes:
Hello,

I have 2 files:

source/test.d:
module foo.test;

and

source/bar.d
module foo.bar;
import foo.test;


When I am compiling this 2 files together there is no problem.
But when I compile it with -c flag (LDC) compiler thrown an error 
(cannot find foo/test.d)

Why isn't import path resolved from module declaration when it is 
possible?
module foo.bar;
import foo.test;

Compiler should know foo is the same directory in which bar is.
Jun 10 2016
parent Mike Parker <aldacron gmail.com> writes:
On Friday, 10 June 2016 at 15:20:21 UTC, Satoshi wrote:
 Hello,

 I have 2 files:

 source/test.d:
 module foo.test;

 and

 source/bar.d
 module foo.bar;
 import foo.test;


 When I am compiling this 2 files together there is no problem.
 But when I compile it with -c flag (LDC) compiler thrown an 
 error (cannot find foo/test.d)

 Why isn't import path resolved from module declaration when it 
 is possible?
 module foo.bar;
 import foo.test;

 Compiler should know foo is the same directory in which bar is.
For any module foo, the compiler will look for foo.d in the current directory or on the import path. For any module foo.bar, the compiler will look for foo/bar.d relative to the current directory or on the import path. So if you compile outside of the source directory, this breaks and you must specify an import path for the files to be found. In your case, you have the file source/bar but have named the module foo.bar. Even if you cd source before compiling, the compiler will be looking for foo/bar.d because that's what you have named the module. If you want to use separate compilation, you need to make your module names have the same name as your file names. If you do this: source/foo/bar.d source/foo/test.d Then one option is: dmd -Isource -c source/foo/bar.d And another is: cd source dmd -c foo/bar.d Assuming, of course, that bar.d imports foo.test.
Jun 10 2016