Strange Multiple Definition Error using makefile

LeqxLeqx mitchelldlarson at protonmail.ch
Wed Dec 26 21:21:28 UTC 2018


Hello,

When compiling a simple project with makefile, I'm getting the 
error:
myclass.d:(.text+0x88): multiple definition of 
`_DT16_D15myabstractclass15MyAbstractClass11firstMethodMFZv'

I'm not certain what's actually wrong with my code. The error 
occurs when the all of the original object (*.o) files are linked 
together to make the executable. According to `nm', both the 
files `myabstractclass.o' and `myclass.o' contain a definition of:

0000000000XXXXXX T 
_DT16_D15myabstractclass15MyAbstractClass11firstMethodMFZv

(where the X's are replaced by their offset in the file)

I've distilled the error to the smallest number of files and 
lines that seem to induce it. The 4 source files are as follows:

============ main.d ==============

     module main;
     import myclass;

     int main(string[] args)
     {
       MyClass myClass;
       myClass = new MyClass;
       myClass.firstMethod;
       myClass.secondMethod;
       return 0;
     }


======== myinterface.d ============

     module myinterface;

     interface MyInterface
     {
       void firstMethod();
       void secondMethod();
     }

======= myabstractclass.d =========

     module myabstractclass;
     import myinterface;

     abstract class MyAbstractClass : MyInterface
     {
       void firstMethod()
       {
         import std.stdio : writefln;
         writefln("hello from firstMethod");
       }
     }

============ myclass.d ==========

     module myclass;

     import myabstractclass;

     class MyClass : MyAbstractClass
     {
       void secondMethod()
       {

       }
     }

===== and my simple makefile ======

     O_FILES=myinterface.o myabstractclass.o myclass.o main.o
     CC=gdc

     .PHONY : all
     all : $(O_FILES)
             $(CC) $(O_FILES) -o test.bin

     %.o : %.d
             $(CC) -c -o $@ $<

     .PHONY : clean
     clean :
             rm -f $(O_FILES) test.bin

=================================

I'm using GDC version: `gdc (Ubuntu 8.2.0-1ubuntu2~18.04) 8.2.0'

So ultimately, my question is: What am i doing wrong here?
Any and all help is appreciated.


More information about the D.gnu mailing list