"undefined reference to" error. can't compile with custom modules

Adam D. Ruppe destructionator at gmail.com
Sun Sep 16 07:44:26 PDT 2012


On Sunday, 16 September 2012 at 14:28:40 UTC, Andrey wrote:
> Well, sometimes it works and sometimes doesn't. For example, if 
> I exclude std.stdio import from the module file, then it works 
> just with "dmd tests.d". Although yersterday it worked fine 
> even with imported stdio. It this undefined behavior? What it 
> depends on?

It is defined, but the reason might not be easy to spot. The 
factor that matters is if any of the code in the file is actually 
used by the finished program. If you call one of the 
datastructures functions in tests.d, you will always have to 
include the module.

With imports, there can be seemingly hidden uses of the module 
code in cases like initialization or getting certain class info, 
etc.

When you import std.stdio, it tries to get the struct's typeinfo, 
which it uses to find the toString() method. Since this uses part 
of your module's code, it needs to find the module to link. Thus 
you get the error if it wasn't specified.

Now, sometimes, you can use part of a module, but not need the 
moduleinfo. This would be if you only used a template from it. 
The reason is the template code then ends up in your other module 
via the import, so the linker can find it that object file. In 
these (rare) cases, you won't get an error.

I feel like I'm rambling.. I hope it makes sense.

But, in general, just always list your modules on the command 
line. 99% of the time, you'll need it to successfully link. Some 
part of it is almost always used.


More information about the Digitalmars-d-bugs mailing list