Importing problems

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Feb 13 09:45:08 PST 2013


On Wed, Feb 13, 2013 at 06:17:51PM +0100, Korey Peters wrote:
[...]
> ...and at the terminal:
> me at 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! :)

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

As to the 'why': compiled languages generally are compiled in two steps:
first the compiler parses the source file(s) and generates executable
code for each file (this is generally called 'object code'), then a
second stage, calling 'linking', is used to link these object files
together into the final executable. The 'import' statement only relates
to the first stage, that is, to use declarations in another source file.
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.

Hope this helps.


T

-- 
English is useful because it is a mess. Since English is a mess, it maps
well onto the problem space, which is also a mess, which we call
reality. Similarly, Perl was designed to be a mess, though in the
nicests of all possible ways. -- Larry Wall


More information about the Digitalmars-d-learn mailing list