On 9/25/18 10:13 AM, Jacob Carlborg wrote:
On Tuesday, 25 September 2018 at 12:05:21 UTC, Jonathan M Davis wrote:
If you use -betterC, then it's trivial, because your D program is
restricted to extern(C) functions and features which don't require
druntime. It can also be done without -betterC (and thus with
druntime), but it gets to be _way_ more of a pain, because it requires
that you manually initialize druntime - either by forcing whatever is
using your "C" library to call a specific function to initialize
druntime before using any of its normal functions or by having every
function in the library check whether druntime has been initialized
yet and initialize it if it hasn't been before it does whatever it's
supposed to do.
Shouldn't it be possible to use a C initialization function, i.e.
pragma(crt_constructor) to initialize druntime? Then it only needs to be
initialized once and it's not required to check if it's initialized all
the time.
I don't know if that's the right call. pragma(crt_constructor) tasks run
*before* C main. If you are initializing the runtime, it means you are
running D static ctors.
Since C initialization functions have no order to them, it's possible
that some initialization functions in the D runtime are using
uninitialized pieces of the C runtime. But of course, I'm not sure. I
just wouldn't trust it if it were me. We don't test initializing the D
runtime before C main is started (in which case, the C runtime is
guaranteed to be set up).
Does anyone know if the C runtime is set up before these functions are
run? Is it guaranteed?
-Steve