Initializing D in C to use?

bachmeier via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 17 10:31:16 PDT 2014


On Friday, 17 October 2014 at 17:18:34 UTC, Adam D. Ruppe wrote:
> On Friday, 17 October 2014 at 16:35:46 UTC, Jeremy DeHaan wrote:
>> I remember reading something(but my googlefu is weak today) 
>> about having to initialize the runtime if you are using D 
>> inside another language. Can anyone confirm this is the case?
>
> Yeah, unless the main() is in D, you need to run initialization 
> functions.
>
> The way I recommend doing it is to write your own extern(C) 
> MyLib_Init and MyLib_Term functions. Then have their 
> implementations call the import core.runtime; 
> Runtime.initialize()
>
> http://dlang.org/phobos/core_runtime.html#initialize
>
> This way, you can do other init stuff for your lib too without 
> the user worrying about those details. Furthermore, having to 
> call a library init function is somewhat normal for C libs, so 
> the user shouldn't think much of it.

Just for completeness, here's an example for Googlers that come 
across this, taken from code I write all the time:

import core.runtime;

extern(C) {
   void R_init_libfoo() {
     Runtime.initialize();
   }
}


More information about the Digitalmars-d-learn mailing list