'Undefined reference' linking errors

Joseph Wakeling joseph.wakeling at gmail.com
Wed Apr 7 13:17:27 PDT 2010


Hello everyone,

A probably stupid but I-can't-find-the-solution-with-Google problem.

I'm trying to compile a small project I'm working on to learn D, called
'dregs'.  It's just 3 files for now (2 modules plus a little test program).
Unfortunately every time I try and compile, I get 'undefined reference' errors
for the classes/structs in the modules.

Here are the files:

// reputation.d
module dregs.reputation;

struct rating
{
        uint u;   // user ID
        uint o;   // object ID
        double r; // rating value
}

class reputation
{
        this()
        {
        }
        this(ref rating[] ratings,
             ref double[] reputation_user,
             ref double[] reputation_object)
        {
        }
}
// END

// avg.d
module dregs.avg;

public import dregs.reputation;

class avg_weighted : reputation
{
        double[] weight_sum;
        this(){}
        this(ref rating[] ratings,
             ref double[] reputation_user,
             ref double[] reputation_object)
        {
                weight_sum.length = reputation_object.length;
                foreach(r; ratings) {
                        reputation_object[r.o] += r.r;
                        weight_sum[r.o] += reputation_user[r.u];
                }
                foreach(o, r; reputation_object)
                        r /= weight_sum[o];
        }
        void opCall(ref rating[] ratings,
                    ref double[] reputation_user,
                    ref double[] reputation_object);
}

class avg_arithmetic : avg_weighted
{
        this(ref rating[] ratings,
             ref double[] reputation_user,
             ref double[] reputation_object)
        {
                foreach(r; reputation_user)
                        r = 1;
                super(ratings,reputation_user,reputation_object);
        }
        void something(ref dregs.reputation.rating[] ratings,
                       ref double[] reputation_user,
                       ref double[] reputation_object)
        {
                avg_weighted(ratings,reputation_user,reputation_object);
        }
}
// END

// test.d
import std.stdio;
import dregs.reputation;
import dregs.avg;

void main()
{
        rating[] r;
        double[] reputation_user;
        double[] reputation_object;

        reputation_user.length = 999;
        reputation_object.length = 1;

        foreach(u;0..reputation_user.length) {
                rating _r = {u,0,(u%3)};
                r ~= _r;
        }
}
// END

I'm running dmd 2.042 on Ubuntu 9.10.

   dmd -O -I../ test.d avg.d reputation.d

... produces the following errors:

------------------------------------
test.o:(.rodata+0x98): undefined reference to
`_D5dregs3avg12avg_weighted6opCallMFKAS5dregs10reputation6ratingKAdKAdZv'
test.o:(.rodata+0xf8): undefined reference to
`_D5dregs3avg12avg_weighted6opCallMFKAS5dregs10reputation6ratingKAdKAdZv'
test.o: In function
`_D5dregs3avg14avg_arithmetic9somethingMFKAS5dregs10reputation6ratingKAdKAdZv':
reputation.d:(.text._D5dregs3avg14avg_arithmetic9somethingMFKAS5dregs10reputation6ratingKAdKAdZv+0x1b):
undefined reference to
`_D5dregs3avg12avg_weighted6opCallMFKAS5dregs10reputation6ratingKAdKAdZv'
collect2: ld returned 1 exit status
--- errorlevel 1
------------------------------------

This surely is something very basic, but I couldn't find a reason for it in my
search of the archives ... :-(  Can anyone advise what I'm doing wrong?

Thanks & best wishes,

    -- Joe


More information about the Digitalmars-d-learn mailing list