| Thread overview | ||||||
|---|---|---|---|---|---|---|
| 
 | 
| February 05, 2003C linking to D library ? | ||||
|---|---|---|---|---|
| 
 | ||||
| Can a C program be linked against a LIB built with D? | ||||
| February 05, 2003Re: C linking to D library ? | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to int_80h | int_80h wrote:
> Can a C program be linked against a LIB built with D?
> 
Sure.
you write in D something like
extern(C) return_type functionName(type1 name1, type2 mane2)
{
	// function does smth here
}
such a function can be directly consumed by a C programme. You might have an (OS-dependant) choice of other calling conventions, noted by extern(convention), which you might also want to use in your C program, "Pascal" and "Windows". Windows is known to you as "stdcall". The difference between both is AFAIK parameter order, and they are both faster and safer than the C calling convention, but probably not as fast as D internal calling convention. The drawback of "Windows" and "Pascal" is that they don't allow var_args, which are to be avoided anyway.
-i.
 | |||
| February 05, 2003Re: C linking to D library ? | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to Ilya Minkov | Oh, i didn't try, but it might be possible to write
extern(convention) {
...
}
and place all your library functions instead of "..." (and don't forget to replace "convention"), and thus wrap all at one shot.
-i.
Ilya Minkov wrote:
> int_80h wrote:
> 
>> Can a C program be linked against a LIB built with D?
>>
> 
> Sure.
> 
> you write in D something like
> 
> extern(C) return_type functionName(type1 name1, type2 mane2)
> {
>     // function does smth here
> }
> 
> such a function can be directly consumed by a C programme. You might have an (OS-dependant) choice of other calling conventions, noted by extern(convention), which you might also want to use in your C program, "Pascal" and "Windows". Windows is known to you as "stdcall". The difference between both is AFAIK parameter order, and they are both faster and safer than the C calling convention, but probably not as fast as D internal calling convention. The drawback of "Windows" and "Pascal" is that they don't allow var_args, which are to be avoided anyway.
> 
> -i.
> 
 | |||
| February 05, 2003Re: C linking to D library ? | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to int_80h | int_80h wrote:
> Can a C program be linked against a LIB built with D?
You need to use extern (C) as Ilya says, but you also need to boot up the runtime with this in C:
   extern void gc_init();
   extern void gc_term();
   extern void _minit();
   extern void _moduleCtor();
   extern void _moduleUnitTests();
   void Dinit ()
   {
       gc_init ();
       _minit ();
       _moduleCtor ();
       _moduleUnitTests ();
   }
   void Dterm ()
   {
       gc_term ();
   }
Global pointers shared with D might actually work properly, although using the same tool in C (DMC) would increase the chances of that.
There're other concerns.  It's not a well-trod path.
 | |||
Copyright © 1999-2021 by the D Language Foundation
  Permalink
Permalink Reply
Reply