digitalmars.D.learn - Initializing D in C to use?
- Jeremy DeHaan (9/9) Oct 17 2014 I'm starting a new project, and I plan on creating a C interface
- K.K. (9/18) Oct 17 2014 Sorry if this isn't the most helpful answer but.. Do you have
- Adam D. Ruppe (6/8) Oct 17 2014 buy my book too, and write amazon reviews :P
- Jeremy DeHaan (4/12) Oct 17 2014 I actually do own it!
- Adam D. Ruppe (11/14) Oct 17 2014 Yeah, unless the main() is in D, you need to run initialization
- bachmeier (9/24) Oct 17 2014 Just for completeness, here's an example for Googlers that come
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
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! JeremySorry 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
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
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: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!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
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
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: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(); } }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