ldc
ldc copied to clipboard
`-defaultlib=phobos2-ldc-shared,druntime-ldc-shared` breaks static constructor
Given two files, "a.d":
import std.stdio;
static this() { writefln!"static this()"; }
And "b.d":
import a;
void main() { }
When I build a binary
ldc2 a.d b.d -oftest -defaultlib=phobos2-ldc-shared,druntime-ldc-shared
./test
Then the static constructor is not called.
If I call ldc2 with -link-defaultlib-shared instead, it works.
When module constructors are silently not called, the bug can take a long time to identify, and the resolution yet longer. If -defaultlib=...-shared can't work on its own, then LDC should just error if a -shared lib is passed to -defaultlib.
From the v1.29 changelog:
When linking manually (not via LDC) against shared druntime, it is now required to link the bundled
lib/ldc_rt.dso.o[bj]object file into each binary. It replaces the previously Windows-specificdso_windows.obj. (#3850)
To be precise, this is required when not using -link-defaultlib-shared (which additionally takes care of setting a default rpath when linking). I didn't expect anyone to link like you did.
I just followed the error messages from DMD's commandline. (-defaultlib=libphobos2.so). With other flags, LDC tries to minimize the difference in invocation to DMD, like -of.
Ie. I started with dmd -defaultlib=libphobos2.so, changed it to ldc2 -defaultlib=libphobos2.so, then fixed issues as they arose. This did not lead me to -link-defaultlib-shared, but it did lead me to this bug. :)