Shared library with C API

Etienne via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Fri May 30 10:37:04 PDT 2014


On 2014-05-26 12:33 PM, Kai Nacke wrote:
>
> No. Just call rt_init() before you call the first D function. You should
> also call rt_term() e.g. on program shutdown. This makes sure that the D
> runtime is initialized properly and that resources are freed on shutdown.
> You should also make sure that no D exception escapes as C has no
> exception handling and D exceptions are not compatible with C++ exceptions.
>
> Regards,
> Kai

I looked at the p.97 in the D Cookbook, and it calls rt_init() from C.

I was wondering, isn't it easier to put this initialization in a static 
this(), in the D module directly?

e.g. in test.d

import std.stdio, core.stdc.stdio : stderr;
extern(C) void helloC() nothrow; // a function from C

extern(C)
void helloD() nothrow {
	helloC();
	try {
		writeln("hello from D!");
	} catch(Throwable t) {
		fprintf(stderr, "writeln threw an exception.\n");
	}
}

static this(){
	rt_init();
}


More information about the digitalmars-d-ldc mailing list