Thread overview
Can D code be linked as part of C programs? (plug-ins in D?)
Feb 28, 2009
kll
Mar 02, 2009
kll
February 28, 2009
Can D code be linked as part of C programs?

e.g. can I write Apache module (dynamically linked .so file) using D?

I wonder if that's a matter of sneaking in D's runtime, starting GC, etc., or is it not possible at all?


February 28, 2009
On Sat, Feb 28, 2009 at 3:40 PM, kll <kl@mailinator.com> wrote:
> Can D code be linked as part of C programs?
>
> 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.
March 02, 2009
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?
March 02, 2009
On Mon, Mar 2, 2009 at 7:57 AM, kll <kl@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.