www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initializing D in C to use?

reply "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
I'm starting a new project, and I plan on creating a C interface 
in case other languages want to use it. 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?

I just wanted to make sure I make that explicit for users if they 
don't want to use the library with D.

Thanks all!
    Jeremy
Oct 17 2014
next sibling parent reply "K.K." <trampzy yahoo.com> writes:
On Friday, 17 October 2014 at 16:35:46 UTC, Jeremy DeHaan wrote:
 I'm starting a new project, and I plan on creating a C 
 interface in case other languages want to use it. 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?

 I just wanted to make sure I make that explicit for users if 
 they don't want to use the library with D.

 Thanks all!
    Jeremy
Sorry if this isn't the most helpful answer but.. Do you have Adam Ruppe's book? On pg96, "Writing part of a C program in D" it sounds like he does what you're asking. I was gonna write more, but being I haven't tried it before, I don't wanna start giving potentially false info ...I mean I could write more but it's probably a bad idea getting help from me xD
Oct 17 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 17 October 2014 at 17:14:30 UTC, K.K. wrote:
 Sorry if this isn't the most helpful answer but.. Do you have
 Adam Ruppe's book?
buy my book too, and write amazon reviews :P A lot of the topics in there were chosen because there are questions that come up somewhat often on this forum or the D chat room. I answer a lot of questions here too, but the book is cool too!
Oct 17 2014
parent "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
On Friday, 17 October 2014 at 17:21:11 UTC, Adam D. Ruppe wrote:
 On Friday, 17 October 2014 at 17:14:30 UTC, K.K. wrote:
 Sorry if this isn't the most helpful answer but.. Do you have
 Adam Ruppe's book?
buy my book too, and write amazon reviews :P A lot of the topics in there were chosen because there are questions that come up somewhat often on this forum or the D chat room. I answer a lot of questions here too, but the book is cool too!
I actually do own it! I just haven't had as much time as I would like to go through it. Thanks for the answers everyone!
Oct 17 2014
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Oct 17 2014
parent "bachmeier" <no spam.com> writes:
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(); } }
Oct 17 2014