Writing a library

David Gileadi foo at bar.com
Fri Feb 16 09:26:00 PST 2007


Jarrett Billingsley wrote:
> "Mn" <mn at mailinator.com> wrote in message 
> news:er492f$1sti$1 at digitalmars.com...
>> Hello World!
>>
>> Is it possible to write a library in D that can be used by other 
>> programming languages? And if yes, how to do it? I can think of two ways 
>> of "using" a lib in general:
>>
>> 1. The OOP way: use a class of the lib, then its functions, dunno how its 
>> called.
>> 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.
>>
>> I am only interested in the more popular languages like C, C++, Java, C#, 
>> PHP.
>>
>> Greetings and thank you.
>> -- Mn
> 
> No other languages understand D calling or mangling conventions, but D can 
> make functions with C, Windows, and Pascal calling conventions.  If you just 
> do something like:
> 
> extern(C) export void func(int x) { ... }
> 
> You can then, maybe, make a DLL or something out of it which can be called 
> from virtually any other mainstream language, since most things understand 
> the C calling convention. 
> 

I tried this very thing about a year ago, creating a DLL in D with 
extern(C) functions which was imported and called by a C# program.  It 
passed data back and forth via simple structs, defined on both the C# 
and D side.  It worked great when called via a D host, but would 
segfault every time I tried it from the C# host.  I never did figure out 
what was going on (and since it was a school project I just gave up and 
ported the code to C#).  I suspected it was the fault of the garbage 
collector running in a separate thread, and at the time the calls to 
disable/enable the GC didn't do anything, so I didn't get to test if my 
suspicion was true.

I'd be very interested to hear of your success with this.



More information about the Digitalmars-d mailing list