www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about linker errors when using slices

reply tspike <email gamedevgp.net> writes:
I’ve been using D for personal projects for almost a year now and 
I really love it. I recently ran across a linker error that I’m a 
little confused by. Consider the following files:

platform.d:

     module platform;

     import app;

     struct PlatformData
     {
         AppData a;
     }

     void main()
     {

     }

app.d:

     module app;

     struct AppData
     {
         //int* items;
         int[] items;
     }

If you only compile platform.d, the linker will complain about 
“undefined references.” This is true when using dmd and gdc, 
though platform.d compiles just fine when using ldc. But the file 
only fails to compile when the “items” member of AppData is a 
slice; if “items” is an int* platform.d will compile.

The linker spits the following:

     platform.o:(.data._D22TypeInfo_S3app7AppData6__initZ+0x30): 
undefined reference to `_D3app7AppData9__xtoHashFNbNeKxSQBeQBdZm'
     platform.o:(.data._D22TypeInfo_S3app7AppData6__initZ+0x38): 
undefined reference to 
`_D3app7AppData11__xopEqualsFKxSQBdQBcKxQjZb'

I was just wondering if anyone knows if this behavior is expected 
or if this is a compiler bug. Thank you in advance for your time!

PS: I hope this is the right sub-forum for asking this sort of 
question!
Sep 18 2020
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2020-09-19 04:45, tspike wrote:
 I’ve been using D for personal projects for almost a year now and I 
 really love it. I recently ran across a linker error that I’m a little 
 confused by. Consider the following files:
 
 platform.d:
 
      module platform;
 
      import app;
 
      struct PlatformData
      {
          AppData a;
      }
 
      void main()
      {
 
      }
 
 app.d:
 
      module app;
 
      struct AppData
      {
          //int* items;
          int[] items;
      }
 
 If you only compile platform.d, the linker will complain about 
 “undefined references.” This is true when using dmd and gdc, though 
 platform.d compiles just fine when using ldc. But the file only fails to 
 compile when the “items” member of AppData is a slice; if “items” is
an 
 int* platform.d will compile.
 
 The linker spits the following:
 
      platform.o:(.data._D22TypeInfo_S3app7AppData6__initZ+0x30): 
 undefined reference to `_D3app7AppData9__xtoHashFNbNeKxSQBeQBdZm'
      platform.o:(.data._D22TypeInfo_S3app7AppData6__initZ+0x38): 
 undefined reference to `_D3app7AppData11__xopEqualsFKxSQBdQBcKxQjZb'
 
 I was just wondering if anyone knows if this behavior is expected or if 
 this is a compiler bug. Thank you in advance for your time!
You should compile both files. I'm guessing LDC might be doing some form of optimization to figure out that it doesn't need those symbols.
 PS: I hope this is the right sub-forum for asking this sort of question!
Yes. -- /Jacob Carlborg
Sep 18 2020
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/18/20 10:45 PM, tspike wrote:

 If you only compile platform.d, the linker will complain about 
 “undefined references.” This is true when using dmd and gdc, though 
 platform.d compiles just fine when using ldc. But the file only fails to 
 compile when the “items” member of AppData is a slice; if “items” is
an 
 int* platform.d will compile.
 
 The linker spits the following:
 
      platform.o:(.data._D22TypeInfo_S3app7AppData6__initZ+0x30): 
 undefined reference to `_D3app7AppData9__xtoHashFNbNeKxSQBeQBdZm'
      platform.o:(.data._D22TypeInfo_S3app7AppData6__initZ+0x38): 
 undefined reference to `_D3app7AppData11__xopEqualsFKxSQBdQBcKxQjZb'
 
 I was just wondering if anyone knows if this behavior is expected or if 
 this is a compiler bug. Thank you in advance for your time!
On one hand, I don't necessarily expect it. These are symbols that are used to build an appropriate TypeInfo instance. I wouldn't expect it, because you aren't using TypeInfo anywhere. However, it does happen quite a bit, because the conditions under which D generates or expects a generated TypeInfo are somewhat obscure and not documented. It's probably why LDC works and the others don't. I hope Andrei's recent push to demystify TypeInfo stuff makes this a lot more tractable. The answer is -- just build with all the files. The linker should throw out stuff that isn't needed.
 PS: I hope this is the right sub-forum for asking this sort of question!
Of course! -Steve
Sep 19 2020