www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - "undefined reference to" error. can't compile with custom modules

reply "Andrey" <andr-sar yandex.ru> writes:
DMD version 2.60 (Linux, 32/64 bit). Ubuntu 12.04, Linux 
3.2.0-30-generic

File "datastructures.d":

module datastructures;

import std.container;
import std.stdio;

struct MyStruct(T) {
T* element;
}

File "tests.d":

import datastructures;

void main() {
	int i = 0;
}

I get this message in console when try to compile both with DMD 
and GDC:

tests.o:(.data+0xc): undefined reference to 
`_D14datastructures12__ModuleInfoZ'
collect2: выполнение ld завершилось с 
кодом возврата 1
--- errorlevel 1
Sep 16 2012
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
I think this is the wrong newsgroup. Normally you'd use 
digitalmars.D.learn.

But when compiling you should put all your custom modules on the 
command line.

dmd tests.d datastructures.d

And that will solve it. Alternatively you can compile them 
separately then put all the .o files together to link, but the 
easiest way is to just list them at once.
Sep 16 2012
prev sibling next sibling parent "Andrey" <andr-sar yandex.ru> writes:
On Sunday, 16 September 2012 at 14:13:02 UTC, Adam D.

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?
Sep 16 2012
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Sep 16 2012
prev sibling parent "Andrey" <andr-sar yandex.ru> writes:
Thank you for clearance. ^_^ I hope this will find its way 
somewhere to official docs.

I suppose, this topic is closed then if there is nothing more to 
add.
Sep 16 2012