Interfacing C with D...How to?

Dan murpsoft at hotmail.com
Mon Apr 23 09:44:32 PDT 2007


Dié Wrote:

> 
> Hello!
> 
> The super guide "D Specification" is not so super sometimes...
> 
> So, someone has worked with C code in D? 
> 
> i haved coded a little example to try this feature.
> 
> 
> Simple c file with one fuction:
> // test.c
> #include "htest.h"
> 
> void printFromC(){
>   printf("Hello World (printed from C)");
> }
> 
> his header file:
> // htest.h
> void printFromC();
> 
> header file converted in d:
> /* Converted to D from htest.h by htod */
> // htest.d
> module htest;
> //#include <iostream>
> 
> //C     void printFromC();
> extern (C):

That will make everything following "extern(C)" until you declare otherwise.  I think all you really wanted was:

extern(C) void printFromC(void);

> void  printFromC(void);
> 
> d main file:
> // main.d
> import htest;
> int main(char[][] args){
> 		printFromC();
> }
> 
> I have no idea if i've coded correctly, and how copile/link...?????
> 
> Someone can help me?
> 
> Tks! :-P
> 
> Dié

So you're aware, the entirety of the c standard library is available by implementing this; all they did was extern(C): and list off all the C functions.  Don't take my word for it, look at:

*/dmd/src/phobos/std/c/

Note that I didn't actually look up this path, I'm at work, so that's just from memory.

Best of luck.



More information about the Digitalmars-d mailing list