www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does calling a struct constructor generate linker errors when

reply Stijn <stijn.herreman telenet.be> writes:
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?
Dec 23 2017
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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.
Dec 23 2017
parent reply Stijn <stijn.herreman telenet.be> writes:
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.
background, so I hadn't thought of doing that. Thanks!
Dec 23 2017
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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.
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.
Dec 23 2017
parent Stijn <stijn.herreman telenet.be> writes:
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.
Dec 23 2017