June 10, 2011
On 2011-06-10 14:28, Joshua Niehus wrote:
> Hello,
> 
> I am trying to compile code which is composed of two modules (in the same
> directory):
> 
> main.d
> foo.d
> 
> foo.d just declares a class Foo which has a string variable "bar" which i set to "hello" and main.d just prints bar's value to the console:
> 
> // --------- main.d -----------
> import std.stdio, foo;
> 
> void main() {
> Foo f = new Foo;
> writeln(f.bar);
> }
> 
> When i attempt to compile main.d via:
> $dmd main.d
> I get undefined symbol errors.
> 
> But when I run:
> $rdmd main.d
> it works as expected.
> 
> The only way I could get main.d to compile with just dmd was to compile foo
> as a lib first and then compile main.d and passing the foo.a file as an
> argument:
> $dmd -lib foo.d
> $dmd main.d foo.a
> 
> Is this normal?
> I got the impression from TDPL (Alexandrescu) that the dmd compiler would
> automatically search the root directory, find foo.d, work its magic, and
> create all the symbols that were defined in foo.d for main.d to compile...

With dmd, you must list every file that you're compiling. The only exceptions are that when dealing with libraries, dmd to have the root directory where the source is passed to -I, and it needs to have the root directory where the library is given with -L and -l with the library name (or just the library directly if you don't want it to search for the library). It's like gcc and dmc in that respect. It does nothing extra to track down files to compile for you. It'll find the source for importing thanks to -I, but it won't compile it. You must still compile it. You don't normally need -I or -L however, because dmd.conf (or sc.ini on Windows) already adds the appropriate flags for Phobos for you. You only need too specify them yourself when using other libraries.

rdmd does extra magic to automatically track down all of the files that main.d imports and compile them. dmd doesn't do that.

- Jonathan M Davis
June 10, 2011
Perhaps -L is just a linux feature, on Windows it only passes options to Optlink, and passing directories won't add them to search paths for library files.

E.g. if I have:
main.d
foo/test.d
foo/test.lib <- compiled as a library

and try to invoke:
dmd main.d -I.\foo\ test.lib -L.\foo\

Optlink won't find the lib file:
 Warning 2: File Not Found foo.lib

I always have to specify the full path to the lib file, e.g.: dmd main.d -I.\test2\ .\test2\foo.lib