www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Interfacing C with D...How to?

reply =?ISO-8859-1?Q?Di=e9?= <d.cavadini freesurf.ch> writes:
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):
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é
Apr 23 2007
next sibling parent Dan <murpsoft hotmail.com> writes:
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.
Apr 23 2007
prev sibling parent torhu <fake address.dude> writes:
Dié wrote:
<snip>
 header file converted in d:
 /* Converted to D from htest.h by htod */
 // htest.d
 module htest;
 //#include <iostream>
 
 //C     void printFromC();
 extern (C):
 void  printFromC(void);
extern (C): void printFromC(); // no void parameter
 I have no idea if i've coded correctly, and how copile/link...?????
 
 Someone can help me?
If you're on windows, you have to compile the C file with dmc to get a file that you can link with dmd's output. Like this: dmc -c htest.c And then compile the D file, and link them together to produce main.exe: dmd main htest.o Download (DMC) Digital Mars C compiler here: http://www.digitalmars.com/download/dmcpp.html On linux, you'd probably do the same thing, but use gcc instead of dmc.
Apr 23 2007