[Bug 199] unittests not linked unless module is imported

via D.gnu d.gnu at puremagic.com
Mon Sep 21 05:12:20 PDT 2015


Johannes Pfau <johannespfau at gmail.com> changed:

           What    |Removed                     |Added
             Status|NEW                         |ASSIGNED
                 CC|                            |johannespfau at gmail.com

--- Comment #1 from Johannes Pfau <johannespfau at gmail.com> ---
I think we can't fix this. Does this work with DMD or LDC?

The problem is this: We already build the ModuleInfo chain by emitting one c
constructor per module. This means all unittests are referenced by ModuleInfos
which are referenced by c constructors which are always kept in the executable
by linker scripts. This enures that, as long as you link all objects
explicitly, all unittests will be linked in, even without explicitly importing

However, this does not work for code in static libraries because c constructors
in static libraries do not work:

The linker only looks at object files in static libraries if it searches an
undefined symbol. If all symbols are defined, it does not search the object

Now you want this to work without imports in your main application. But how
could gdc know, when compiling the main applications which additional modules
might exists in a static library? If we know module X exists we can add an
reference to force the linker. But in your case we explicitly do not know about
additional modules so we can't add a reference.

You can use the same workarounds C++ uses though:
Use it with GDC like this:
gdc -funittest main.d -Wl,--whole-archive libtest.a -Wl,--no-whole-archive

You are receiving this mail because:
You are watching all bug changes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d.gnu/attachments/20150921/424f5a5d/attachment.html>

More information about the D.gnu mailing list