Discussion:
Initializing D in C to use?
Jeremy DeHaan via Digitalmars-d-learn
2014-10-17 16:35:45 UTC
Permalink
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
K.K. via Digitalmars-d-learn
2014-10-17 17:14:30 UTC
Permalink
Post by Jeremy DeHaan via Digitalmars-d-learn
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
Adam D. Ruppe via Digitalmars-d-learn
2014-10-17 17:21:10 UTC
Permalink
Post by K.K. via Digitalmars-d-learn
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!
Jeremy DeHaan via Digitalmars-d-learn
2014-10-18 01:35:49 UTC
Permalink
Post by Adam D. Ruppe via Digitalmars-d-learn
Post by K.K. via Digitalmars-d-learn
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!

Adam D. Ruppe via Digitalmars-d-learn
2014-10-17 17:18:33 UTC
Permalink
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.
bachmeier via Digitalmars-d-learn
2014-10-17 17:31:16 UTC
Permalink
Post by Adam D. Ruppe via Digitalmars-d-learn
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();
}
}
Loading...