www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ModuleInfo linker error when importing std.stdio and using package.d

reply Bryce Kellogg <kellogg uw.edu> writes:
Hi everyone, I'm new to D, and I'm trying to get a handle on the 
correct way to use packages, modules, and importing things. I 
created a simple example, but I'm getting linker errors in weird 
situations. I'm hoping to get some insight into why the error is 
happening and best practices in these sorts of situations.

I've got a simple project set up as follows:
.
├── dub.json
└── source/
     ├── app.d
     └── my_package/
         ├── my_module.d
         └── package.d


The contents of dub.json are pretty simple:


{
     "name": "my_package_test",
     "description": "A minimal D application.",
     "copyright": "Copyright © 2016, kellogg",
     "authors": ["kellogg"],
     "dependencies": {
     }
}


In app.d, all I do is import my package and instantiate my class.

import my_package;
void main() {
     my_module m = new my_module();
}


And here's my_module.d

module my_package.my_module;
import std.stdio;
class my_module {
     this() {
         writeln("Hello World!");
     }
}



Finally, a one line package.d:

public import my_package.my_module;


Building this via dub results in the error:

.dub/obj/app.o:(.data._D3app12__ModuleInfoZ[_D3app12__ModuleInfoZ]+0x10):
undefined reference to `_D10my_package12__ModuleInfoZ'
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1
ldc2 failed with exit code 1.


Interestingly, if I remove the 'import std.stdio;' and writeln() 
from my_module.d, it compiles fine. It also works if I move the 
import down to inside the constructor, or replace the import in 
app.d with 'import my_package.my_module;' (side stepping the 
package.d).


To summarize: importing my package via package.d results in a 
linker error if I import std.stdio at the top level in my module. 
I'm wondering: what does this error mean, what does it have to do 
with std.stdio, and am I even using package.d correctly?

Thanks!
Nov 08 2016
parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne 8.11.2016 v 21:16 Bryce Kellogg via Digitalmars-d-learn napsal(a):

 ...
 Finally, a one line package.d:

 public import my_package.my_module;
Change it to: module my_package; public import my_package.my_module; Btw, having class name same as module name is not best way, there could be some issues with that
Nov 08 2016