Using D as a shared library.

Jacob Carlborg doob at me.com
Tue May 14 09:01:57 PDT 2013


On 2013-05-14 15:53, Paolo & Kevin wrote:
> I am trying to use a D shared library with a C program.
> But I have a problem, the code works in a D program.
> But it crashes as a library.
>

> What I am doing wrong?
> I think there is something the D language does before starting main.
> And the C language does not. Can I initialize the program correctly
> manually? Or it is another problem altogether?

The D runtime does quite a lot before running D main. You would need to 
a least initialize the runtime. Here's the C main function that is run 
before D main:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L384

Here's the function to you can call to initialize the runtime from C:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L281

And to terminate:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L312

I'm not sure if all this work yet. Using a D shared library from a D 
application, "statically" linked, should work on Linux 64bit. Don't know 
if it works from a C application.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list