Can D code be linked as part of C programs? (plug-ins in D?)

Jarrett Billingsley jarrett.billingsley at gmail.com
Mon Mar 2 06:18:47 PST 2009


On Mon, Mar 2, 2009 at 7:57 AM, kll <kl at mailinator.com> wrote:
> Jarrett Billingsley Wrote:
>
>> > e.g. can I write Apache module (dynamically linked .so file) using D?
>>
>> Sure.
>>
>> > I wonder if that's a matter of sneaking in D's runtime, starting GC, etc., or is it not possible at all?
>>
>> That's precisely what you do.  If you compile the D piece of your
>> program as a lib, including its runtime, you should be able to link C
>> code to it.  You'll have to call the runtime startup/teardown
>> functions yourself, but it should work.  I'm pretty sure people have
>> made Apache modules in D before.
>
> Sorry, I couldn't find information how to do this (I'm a D noob). D site has only info how to link C with D, not D with C.
>
> It seems that DLL's a close to what I need (and I've found Apache module that uses them), but I'm not interested in Windows platform at all, I'm looking for Linux and OS X solution.
>
> Where should I look for this?

It's strange, there doesn't seem to be much info on building shared
libraries on Linux or OSX.  It's actually pretty simple to do so.

For one, I know you can't use DMD on Linux to build SOs.  You'll have
to use either GDC or LDC.  I don't know what the situation is on OSX,
but perhaps DMD will build dylibs correctly there since Walter's done
a lot more work on making it generate correct position-independent
code.  I have no idea, though.

To build an SO, you more or less just build your library as normal,
and just link it a bit differently.  I think DSSS/rebuild will also
insert the init/fini code necessary for setting up and tearing down
the runtime when the SO is loaded and unloaded.  That's probably your
best bet.



More information about the Digitalmars-d mailing list