Thread overview
[Issue 12238] Using pthread_mutex_t.init results in an undefined reference linker error.
Aug 14, 2014
Joakim
Jan 07, 2016
genede@hotmail.com
Jan 07, 2016
genede@hotmail.com
Dec 17, 2022
Iain Buclaw
August 14, 2014
https://issues.dlang.org/show_bug.cgi?id=12238

--- Comment #1 from Joakim <joakim@airpost.net> ---
Created attachment 1385
  --> https://issues.dlang.org/attachment.cgi?id=1385&action=edit
nested union declaration causes incorrect compile if imported from another file

I just ran into this bug when trying to port some Android system headers from C, except with nested unions in my case and with dmd-git-24e8347f1fa4 from a week ago.  I can confirm that Kapps's testcase also doesn't work on linux/x86, when tried with the latest stock dmd 2.065.

I've attached a couple source files to demonstrate the problem.  The structs defined are exactly the ones I'm trying to port from Android, ASensorVector and ASensorEvent shown here:

https://android.googlesource.com/platform/frameworks/native/+/jb-release/include/android/sensor.h

I build and run as so on linux/86 using stock dmd 2.065:

> clang -c foo.c
> dmd food.d foo.o
>./food

That works, since it's getting the struct declarations from the main source file.  This doesn't:

> dmd -version=do_import food.d foo.o

food.o:food.d:function _Dmain: error: undefined reference to '_D4fooi5foogo6__initZ'

Looking at the symbols generated, it appears that dmd is generating the right
symbols only if the union or nested union is declared in the local source file.
 Here's the symbols without do_import:

> dmd -c food.d
> nm -s food.o | grep foogo
00000000 V _D21TypeInfo_S4food5foogo6__initZ
00000000 T _D4food5foogo11__xopEqualsUKxS4food5foogoKxS4food5foogoZb
00000030 R _D4food5foogo6__initZ

With the do_import:
> dmd -version=do_import -c food.d
> nm -s food.o | grep foogo
         U _D4fooi5foogo6__initZ

It's that undefined symbol that causes the linker to fail.  Putting the ASensorVector/ASensorEvent declarations in the same file also gets it to work on Android/x86.

--
June 09, 2015
https://issues.dlang.org/show_bug.cgi?id=12238

Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|unspecified                 |D2

--
January 07, 2016
https://issues.dlang.org/show_bug.cgi?id=12238

genede@hotmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |genede@hotmail.com

--- Comment #2 from genede@hotmail.com ---
Workaround:

I ran into this today with the Windows COM VARIANT struct, which is a union that contains a struct that contains a union and a struct.

I ended up working around this with a separate initialization method:

export static tagVARIANT createVARIANT { return tagVARIANT(); }

This work around isn't elegant, but my guess is it can be broadly used for this issue, potentially blocking some people who are stumped.

--
January 07, 2016
https://issues.dlang.org/show_bug.cgi?id=12238

--- Comment #3 from genede@hotmail.com ---
(In reply to genede from comment #2)
> Workaround:
> 
> I ran into this today with the Windows COM VARIANT struct, which is a union that contains a struct that contains a union and a struct.
> 
> I ended up working around this with a separate initialization method:
> 
> export static tagVARIANT createVARIANT { return tagVARIANT(); }
> 
> This work around isn't elegant, but my guess is it can be broadly used for this issue, potentially blocking some people who are stumped.

or rather unblocking....

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=12238

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P3

--