Linking D Runtime

Jonathan Levi catanscout at gmail.com
Sat Aug 24 02:10:19 UTC 2019


On Saturday, 24 August 2019 at 01:19:01 UTC, H. S. Teoh wrote:
> Not sure what's the "right" approach, but you could try 
> compiling with dmd -v to find the path(s) to the runtime 
> libraries that you'll need to link.

Oh, cool, I managed to find them on my current system.  I would 
love a more portable solution though.  This should work for now.

> Keep in mind that before calling any non-trivial D code (i.e., 
> that uses the GC or any druntime function, including language 
> hooks) you need to call core.runtime.rt_init first.  And 
> preferably at shutdown call core.runtime.rt_term to cleanup.

Yup.

> Not sure what will happen if more than one D library does this, 
> though. My guess is that you'll somehow need to get them all to 
> link to (or otherwise be aware of) a single instance of the D 
> runtime, otherwise you might get some weird runtime behaviour 
> when the two runtimes conflict with each other.

I should only be executing the code from one place so it should 
not be a problem.  But `rt_init` and `rt_term` are just C 
bindings[1] to `core.runtime.Runtime.initialize`[2] and 
`core.runtime.Runtime.terminate` which state that "Each call to 
initialize must be paired by a call to terminate."

[1] 
https://github.com/dlang/druntime/blob/master/src/core/runtime.d#L30
[2] https://dlang.org/phobos/core_runtime.html#.Runtime.initialize




More information about the Digitalmars-d-learn mailing list