Thread overview
Why does calling a struct constructor generate linker errors when using Better C?
Dec 24, 2017
Stijn
Dec 24, 2017
rikki cattermole
Dec 24, 2017
Stijn
Dec 24, 2017
rikki cattermole
Dec 24, 2017
Stijn
December 24, 2017
https://dlang.org/spec/betterc.html doesn't mention struct constructors not working, but I'm getting linker errors when trying to call a struct constructor.

Consider the following program betterc.d

    struct foo
    {
    }

    extern(C) void main()
    {
        auto bar = new foo();
    }

Compile with: dmd -c -m32 betterc.d -betterC
Link with: gcc -m32 betterc.o -Wl,--build-id=none -nostdlib -o "betterc.bin"

Linker output:

    betterc.o: In function `main':
    betterc.d:(.text.main[main]+0xa): undefined reference to `_d_newitemT'
    betterc.o:(.data._D22TypeInfo_S7betterc3foo6__initZ+0x0): undefined reference to `_D15TypeInfo_Struct6__vtblZ'
    collect2: error: ld returned 1 exit status

Is there a mistake in my code, or is the documentation lacking?
December 24, 2017
On 24/12/2017 1:20 AM, Stijn wrote:
> https://dlang.org/spec/betterc.html doesn't mention struct constructors not working, but I'm getting linker errors when trying to call a struct constructor.
> 
> Consider the following program betterc.d
> 
>      struct foo
>      {
>      }
> 
>      extern(C) void main()
>      {
>          auto bar = new foo();
>      }
> 
> Compile with: dmd -c -m32 betterc.d -betterC
> Link with: gcc -m32 betterc.o -Wl,--build-id=none -nostdlib -o "betterc.bin"
> 
> Linker output:
> 
>      betterc.o: In function `main':
>      betterc.d:(.text.main[main]+0xa): undefined reference to `_d_newitemT'
>      betterc.o:(.data._D22TypeInfo_S7betterc3foo6__initZ+0x0): undefined reference to `_D15TypeInfo_Struct6__vtblZ'
>      collect2: error: ld returned 1 exit status
> 
> Is there a mistake in my code, or is the documentation lacking?

new uses GC.
It has nothing to do with structs.
December 24, 2017
On Sunday, 24 December 2017 at 01:21:53 UTC, rikki cattermole wrote:
> On 24/12/2017 1:20 AM, Stijn wrote:
>> [...]
>
> new uses GC.
> It has nothing to do with structs.

Oh I see, simply removing 'new' solves the problem. I've a C# background, so I hadn't thought of doing that. Thanks!
December 24, 2017
On 24/12/2017 1:25 AM, Stijn wrote:
> On Sunday, 24 December 2017 at 01:21:53 UTC, rikki cattermole wrote:
>> On 24/12/2017 1:20 AM, Stijn wrote:
>>> [...]
>>
>> new uses GC.
>> It has nothing to do with structs.
> 
> Oh I see, simply removing 'new' solves the problem. I've a C# background, so I hadn't thought of doing that. Thanks!

Yeah new = memory allocation.

Although maybe you ought to start out with regular D.

-betterC is really there for those who want D but can't have druntime.
They should already know D pretty well.
December 24, 2017
On Sunday, 24 December 2017 at 01:29:56 UTC, rikki cattermole wrote:
> Although maybe you ought to start out with regular D.
>
> -betterC is really there for those who want D but can't have druntime.
> They should already know D pretty well.

I'm using -betterC on purpose, doing bare-metal development. Had to learn a new language anyway, and I had been following D for 8 years or so.
I started out with a stubbed object.d but that turned out to be too hard, so switched to -betterC by recommendation of some people on IRC.