Call C (or C++) with pointer to static function

Simen kjaeraas simen.kjaras at gmail.com
Sun Oct 10 14:10:51 PDT 2010


On Sun, 10 Oct 2010 22:03:51 +0200, Michael Stover  
<michael.r.stover at gmail.com> wrote:

> I am wondering if D would be a good choice for our differential equation
> modeling app.  What it needs to be able to do is dynamically compile D  
> code
> into a static function that can then be used to call C-code that expects  
> a
> pointer to a static function.
>
> Longer description:
> It is a desktop app.  The user writes equations.  I'd like to be  
> translate
> the equations into D code, compile it, and then call C code that  
> represents
> optimizing or solver procedures.  These functions expect a pointer to a
> static function that calculates the values of the equations, which would  
> be
> the D code just compiled.
>
> Is this feasible with D2?

Absolutely. The dynamic compilation part is not directly supported, but
possible. There are several ways of accomplishing this, from Burton
Radon's (outdated, proof-of-concept only) dynamic compilation[1], to
calling the C functions in the generated D code, and pipe the output
where you need it.

Interfacing to C is detailed on this page:
http://digitalmars.com/d/2.0/interfaceToC.html

And can mostly be summarized like this:

Create a D function that is callable from C:
extern( C ) returnType myFunc( /* args */ ) {}

Refer to a C function that is callable from D:
extern( C ) returnType myFunc( /* args */ );


[1]:  
http://members.shaw.ca/burton-radons/The%20Joy%20and%20Gibbering%20Terror%20of%20Custom-Loading%20Executables.html

-- 
Simen


More information about the Digitalmars-d mailing list