www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - calling a D function from C and C++ code

reply Bedros Hanounik <2bedros NOSPAM-gmail.com> writes:
I'm a lot interested in D recently, and have been reading the documentation the
wiki, forums, etc. But I could not find a place where it explains how to call a
D function from C and C++ code

all the documentation...etc discusses how to bind an existing library to D
code; which is the most common case, but how about building a brand new library
written in D; can anyone write a C code (or C++) to interface this library in D.

Thanks,

-Bedros
Aug 25 2007
next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
Bedros Hanounik wrote:
 I'm a lot interested in D recently, and have been reading the
 documentation the wiki, forums, etc. But I could not find a place
 where it explains how to call a D function from C and C++ code
 
 all the documentation...etc discusses how to bind an existing library
 to D code; which is the most common case, but how about building a
 brand new library written in D; can anyone write a C code (or C++) to
 interface this library in D.

As long as the D functions meant to be called from C are given extern (C) linkage, they'll be accessible.
Aug 25 2007
prev sibling next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Bedros Hanounik wrote:
 I'm a lot interested in D recently, and have been reading the
 documentation the wiki, forums, etc. But I could not find a place
 where it explains how to call a D function from C and C++ code
 
 all the documentation...etc discusses how to bind an existing library
 to D code; which is the most common case, but how about building a
 brand new library written in D; can anyone write a C code (or C++) to
 interface this library in D.

Pretty easy conceptually, just make any functions you want to call from C/C++ be "extern(C)", makeing sure you don't use any funky D-only parameter types, and then write the equivalent C prototype in a header file for the C/C++ code to use (and extern "C" it for C++). In practice on Windows there are a few other issues in that DMD generates libs that aren't compatible with MSVC or MinGW. The libs are link-compatible with the output of the Digital Mars C/C++ compiler, though (DMC), which you already have if you installed DMD. If you're using GDC, I think you'll also be ok. MinGW generates MSVC-compatible libs, so the MinGW version of GDC should be ok. I'm not sure how to handle it if you want to use DMD on Windows and call from something besides DMC (like MinGW or MSVC). I think in that case your best bet is to create a DLL from the D code. From there you should be able to use MSVC tools, MinGW tools or whatever to create an import lib in the proper format for the C/C++ compiler you want to use. This is something I've been meaning to try out myself but actually haven't done it myself. I know for instance Kirk uses DLLs to link D code into C/Python as a part of PyD. Hope that helps. --bb
Aug 25 2007
prev sibling next sibling parent Traveler Hauptman <none none.com> writes:
Bedros Hanounik wrote:
 
 all the documentation...etc discusses how to bind an existing library to D
code; which is the most common case, but how about building a brand new library
written in D; can anyone write a C code (or C++) to interface this library in D.
 
 Thanks,
 
 -Bedros

<flamebait> From my occasional lurking it seems like calling native D libraries from other languages is not a priority here. They just wave their hand at extern(C) and go back to their meta-programming. </flamebait> Check out the ABI page to start with. http://digitalmars.com/d/abi.html It gets more complete each year... Last time I played with it using C I was able to use a macro with the Dmangled function name and an extra parameter for the object pointer in certain cases. A bug/feature of the linux dmd compiler I think. If you get inspired and crank out a preprocessor for language X that lets you import .di files and prefix Dlib calls with "extern (D)" please post it. -traveler
Aug 27 2007
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Bedros Hanounik"  wrote
 I'm a lot interested in D recently, and have been reading the 
 documentation the wiki, forums, etc. But I could not find a place where it 
 explains how to call a D function from C and C++ code

 all the documentation...etc discusses how to bind an existing library to D 
 code; which is the most common case, but how about building a brand new 
 library written in D; can anyone write a C code (or C++) to interface this 
 library in D.

 Thanks,

 -Bedros

I've done it with extern(C), but only in passing callback function pointers. For example, to catch signals, you can pass a function pointer to an extern(C) function to the sigaction system call. e.g. (tango style): import tango.stdc.posix.signal; import tango.stdc.posix.unistd; import tango.io.Stdout; extern(C) { void handlesignal(int sig) { Stdout("caught signal ")(sig).newline; } } int main(char[][] args) { sigaction_t sa; sa.sa_handler = &handlesignal; sigaction(SIGINT, &sa, null); while(true) { sleep(10); } return 0; } However, I have not tried compiling C/C++ code that directly depends on D code. I'm hoping not to have to deal with that ever :) -Steve
Aug 28 2007