On Fri, 16 Sep 2011 15:14:50 -0400, Jérôme M. Berger <jeber...@free.fr>
wrote:
Andrej Mitrovic wrote:
I'd like to turn some attention to unittests, which don't seem to work
with static libraries. Consider:
.\main.d:
import mylib.test;
void main()
{
auto x = foo();
}
.\mylib\test.d
module mylib.test;
int foo() { return 1; }
unittest { assert(0); }
$ dmd -unittest main.d mylib\test.d && main.exe
core.exception.asserter...@mylib.test(5): unittest failure
So far so good. It even works if I don't reference foo() from within
main(), even though the "unittest.d" file in Phobos has this comment
about it:
"// Bring in unit test for module by referencing function in it"
I think that comment might be outdated.
But now try it with a static library:
$ cd mylib
$ dmd -unittest -lib test.d
$ cd..
$ dmd -unittest main.d mylib\test.lib && main.exe
The unittests from the library don't run.
There's a bug about this in bugzilla somewhere (I just can't seem to
find it now), but it doesn't seem to be getting much attention. Since
unittests are first-class citizens in D, I really hope we can get this
working soon. Cheers.
I think that it is not a bug, it is a feature (sort of). Could you
look at the assembly generated for your main.d file? My guess is
that it does not reference foo at all, either because the call to
foo was inlined or because it was discarded (since you do not use x
after initializing it).
The reason it works with the first form is that all object files
that are specifically put on the command line are included in the
executable when linking even if they are not used.
The reason it does not work with the library is that library
objects are only included if they are referenced (to save executable
size).
That isn't true, the library is not passed to the linker as a library,
it's passed as an archive of object files.
Forgive my ignorance of OPTLINK syntax, I'll make my point with linux
linker:
dmd main.d mylib/libtest.a -> compile in all object files from libtest.a
dmd main.d -L-Lmylib -L-ltest -> only compile in parts of libtest.a that
are referenced
In spite of all this, such inlining or optimizations are only made if you
use -O or -inline. And I think just importing the module includes it.
-Steve