Importing problems
Hi everyone. I'm new to D, coming from a Java/Python background. I've been reading the excellent The D Programming Language book, and want to now start playing around with D. I'm having an issue with importing. When I have the following file: file ~/src/sample.d: = import std.stdio; class A{ int a = 1; } class B{ A a; int b = 2; this(A a){ this.a = a; } } void main(){ auto a = new A; auto b = new B(a); writeln(b.b); writeln(b.a.a); } ...and then at the terminal window: me@ubuntu:~/src$ rdmd sample.d 1 2 However, if I create a second file to import A from like so: file ~/src/sample.d: = import std.stdio; import sample_a; class B{ A a; int b = 2; this(A a){ this.a = a; } } void main(){ auto a = new A; auto b = new B(a); writeln(b.b); writeln(b.a.a); } file ~/src/sample_a.d: = class A{ int a = 1; } ...and at the terminal: me@ubuntu:~/src$ rdmd sample.d /tmp/.rdmd-1000/rdmd-sample.d-94E53075E2E84D963426A11F2B81FDED/objs/sample.o: In function `_Dmain': sample.d:(.text._Dmain+0xa): undefined reference to `_D8sample_a1A7__ClassZ' collect2: error: ld returned 1 exit status --- errorlevel 1 I feel like I'm missing something fundamental (and I'm guessing it's because I don't have any experience in compiled languages. Can anyone help? It sucks to have everything in one file! :) Ubuntu 12.10 64bit with the latest D packages (downloaded a few days ago). I get the same problems in Win7. Kind regards, Korey
Re: Importing problems
Thanks for your response, H.S.Teoh. On Wednesday, 13 February 2013 at 17:47:09 UTC, H. S. Teoh wrote: You need to specify both files on the command line, so that the linker knows where to find everything: rdmd sample.d sample_a.d Running this from the command line produces (exactly?) the same error... me@ubuntu:~/src$ rdmd sample.d sample_a.d /tmp/.rdmd-1000/rdmd-sample.d-94E53075E2E84D963426A11F2B81FDED/objs/sample.o: In function `_Dmain': sample.d:(.text._Dmain+0xa): undefined reference to `_D8sample_a1A7__ClassZ' collect2: error: ld returned 1 exit status --- errorlevel 1 I tried swapping the order of the file names, but that didn't help. But before you can actually use the stuff in the other file, that other file needs to be compiled as well (the 'import' only imports declarations; it doesn't actually compile the declared code), and it needs to be included in the linking stage so that all references between the source files can be linked properly. Ah. I thought that the purpose of rdmd (as opposed to dmd) was to link the files automatically. Thanks.
Re: Importing problems
Hmm. I moved my two sample files from ~/path/to/where/I/was/working to ~/ and the import worked. This makes me suspect a permissions issue. I'll carry on working in ~/ for now, until I sort my stupidity out! Thanks for your help.
Re: Importing problems
On Wednesday, 13 February 2013 at 19:33:00 UTC, jerro wrote: This solves the issue: rdmd --force sample Hi jerro, That definitely helped. There's still some things I haven't figured out yet about D's importing, but this has got me going. Thank you.