Thread overview
Strange Multiple Definition Error using makefile
Dec 26, 2018
LeqxLeqx
Dec 27, 2018
Sebastien Alaiwan
Dec 28, 2018
Iain Buclaw
Jan 04, 2019
LeqxLeqx
December 26, 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.
December 27, 2018
Could you please post the console output (the exact commands invoked by make) ?
December 28, 2018
On Wed, 26 Dec 2018 at 22:25, LeqxLeqx via D.gnu <d.gnu@puremagic.com> wrote:
>
> ===== and my simple makefile ======
>
>      O_FILES=myinterface.o myabstractclass.o myclass.o main.o
>      CC=gdc
>

Ahem, CC means "C Compiler", you should be using another variable name here. ;-)

>
> 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.

Two object files are claiming to have the one true copy of the thunk to firstMethod.

Switching from separate compilation to compiling all sources in one compilation command will sort that out.

---
D_FILES=myinterface.d myabstractclass.d myclass.d myderived.d main.d CC=gdc

.PHONY : all
all : test.bin

test.bin: $(D_FILES)
        $(CC) -o $@ $^
---

There should be better spec over how thunks (particularly to methods outside the compilation unit) are handled.  Maybe this can be addressed before version 9 release.

-- 
Iain
January 04, 2019
On Friday, 28 December 2018 at 13:41:14 UTC, Iain Buclaw wrote:
> On Wed, 26 Dec 2018 at 22:25, LeqxLeqx via D.gnu <d.gnu@puremagic.com> wrote:
>>
>> ===== and my simple makefile ======
>>
>>      O_FILES=myinterface.o myabstractclass.o myclass.o main.o
>>      CC=gdc
>>
>
> Ahem, CC means "C Compiler", you should be using another variable name here. ;-)
>
>>
>> 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.
>
> Two object files are claiming to have the one true copy of the thunk to firstMethod.
>
> Switching from separate compilation to compiling all sources in one compilation command will sort that out.
>
> ---
> D_FILES=myinterface.d myabstractclass.d myclass.d myderived.d main.d CC=gdc
>
> .PHONY : all
> all : test.bin
>
> test.bin: $(D_FILES)
>         $(CC) -o $@ $^
> ---
>
> There should be better spec over how thunks (particularly to methods outside the compilation unit) are handled.  Maybe this can be addressed before version 9 release.

Okay, I'll switch to the all-source-code way for the moment. For some reason, I never had a problem with this when I was using GDC-6, but with the newer GDC, it suddenly became a problem.

Thank you!