www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Basics of calling C from D

reply "belkin" <belkin yahoo.in.com> writes:
Example: I have this C function that is compiled into a library

//File: factorial.h
int factorial(int n);


//File: factorial.c
#include "factorial.h"

int factorial(int n)
{
     if(n!=1)
      return n*factorial(n-1);
}

Question: How do I use it from D?
Jun 11 2014
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 11 June 2014 at 13:52:09 UTC, belkin wrote:
 Question: How do I use it from D?
Write the prototype in your D file with extern(C): extern(C) int factorial(int n); then just call the function normally in D. Make sure you include all the C object files when you compile the D program too so it all links together.
Jun 11 2014
parent reply simendsjo <simendsjo gmail.com> writes:
On 06/11/2014 03:54 PM, Adam D. Ruppe wrote:
 On Wednesday, 11 June 2014 at 13:52:09 UTC, belkin wrote:
 Question: How do I use it from D?
Write the prototype in your D file with extern(C): extern(C) int factorial(int n); then just call the function normally in D. Make sure you include all the C object files when you compile the D program too so it all links together.
I believe the correct answer should be "Buy my book!".
Jun 11 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 11 June 2014 at 14:11:04 UTC, simendsjo wrote:
 I believe the correct answer should be "Buy my book!".
ah, of course! I should just make a .sig file lol http://www.packtpub.com/discover-advantages-of-programming-in-d-cookbook/book chapter 4 talks about this kind of thing :P
Jun 11 2014
next sibling parent "belkin" <belkin yahoo.in.com> writes:
On Wednesday, 11 June 2014 at 14:22:51 UTC, Adam D. Ruppe wrote:
 On Wednesday, 11 June 2014 at 14:11:04 UTC, simendsjo wrote:
 I believe the correct answer should be "Buy my book!".
ah, of course! I should just make a .sig file lol http://www.packtpub.com/discover-advantages-of-programming-in-d-cookbook/book chapter 4 talks about this kind of thing :P
Thanks. This book is on my "To Buy" list.
Jun 11 2014
prev sibling next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On 06/11/2014 04:22 PM, Adam D. Ruppe wrote:
 On Wednesday, 11 June 2014 at 14:11:04 UTC, simendsjo wrote:
 I believe the correct answer should be "Buy my book!".
ah, of course! I should just make a .sig file lol http://www.packtpub.com/discover-advantages-of-programming-in-d-cookbook/book chapter 4 talks about this kind of thing :P
Yeah, I was skimming through that chapter just minutes before I saw this post :) I must say I really like your writing-style as well as the down-to-earth and precise and concise presentation of the material. So kudos to you! Really looking forward to reading some of the more advanced material as well as seeing your dconf presentation.
Jun 11 2014
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 11 June 2014 at 14:45:22 UTC, simendsjo wrote:
 I must say I really like your writing-style as well as the 
 down-to-earth and precise and concise presentation of the 
 material. So kudos to you!
thanks, don't forget to tell that to amazon review readers too :P
 Really looking forward to reading some of the more advanced 
 material as well as seeing your dconf presentation.
Based on the pace of dconf posting, it'll probably be early July before it is online :(
Jun 11 2014
prev sibling parent Joseph Rushton Wakeling via Digitalmars-d-learn writes:
On 11/06/14 16:22, Adam D. Ruppe via Digitalmars-d-learn wrote:
 On Wednesday, 11 June 2014 at 14:11:04 UTC, simendsjo wrote:
 I believe the correct answer should be "Buy my book!".
ah, of course! I should just make a .sig file lol http://www.packtpub.com/discover-advantages-of-programming-in-d-cookbook/book chapter 4 talks about this kind of thing :P
My copy arrived today. Life is clearly going to be more fun. :-)
Jun 11 2014
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 11 June 2014 at 13:52:09 UTC, belkin wrote:
 Example: I have this C function that is compiled into a library

 //File: factorial.h
 int factorial(int n);


 //File: factorial.c
 #include "factorial.h"

 int factorial(int n)
 {
     if(n!=1)
      return n*factorial(n-1);
 }

 Question: How do I use it from D?
//File: blah.d extern(C) int factorial(int n); //coincidentally identical to the C declaration. void main() { assert(factorial(3) == 6); } $ gcc -c factorial.c -ofactorial.o $ dmd blah.d factorial.o $ ./blah or $ gcc -c factorial.c -ofactorial.o $ ar rcs libfactorial.a factorial.o $ dmd blah.d -L-lfactorial $ ./blah Basically, you just translate the header files from C to D, then link to the C implementation. See http://code.dlang.org/packages/dstep for automatic translation of headers.
Jun 11 2014
parent reply "belkin" <belkin yahoo.in.com> writes:
On Wednesday, 11 June 2014 at 14:02:08 UTC, John Colvin wrote:
 On Wednesday, 11 June 2014 at 13:52:09 UTC, belkin wrote:
 Example: I have this C function that is compiled into a library

 //File: factorial.h
 int factorial(int n);


 //File: factorial.c
 #include "factorial.h"

 int factorial(int n)
 {
    if(n!=1)
     return n*factorial(n-1);
 }

 Question: How do I use it from D?
//File: blah.d extern(C) int factorial(int n); //coincidentally identical to the C declaration. void main() { assert(factorial(3) == 6); } $ gcc -c factorial.c -ofactorial.o $ dmd blah.d factorial.o $ ./blah or $ gcc -c factorial.c -ofactorial.o $ ar rcs libfactorial.a factorial.o $ dmd blah.d -L-lfactorial $ ./blah Basically, you just translate the header files from C to D, then link to the C implementation. See http://code.dlang.org/packages/dstep for automatic translation of headers.
This is great. How practical (reliable ) is it to translate a large and complex header file like oci.h ( the interface for Oracle's database API ) to D?
Jun 11 2014
next sibling parent reply "Colin" <grogan.colin gmail.com> writes:
On Wednesday, 11 June 2014 at 14:28:49 UTC, belkin wrote:
 On Wednesday, 11 June 2014 at 14:02:08 UTC, John Colvin wrote:
 On Wednesday, 11 June 2014 at 13:52:09 UTC, belkin wrote:
 Example: I have this C function that is compiled into a 
 library

 //File: factorial.h
 int factorial(int n);


 //File: factorial.c
 #include "factorial.h"

 int factorial(int n)
 {
   if(n!=1)
    return n*factorial(n-1);
 }

 Question: How do I use it from D?
//File: blah.d extern(C) int factorial(int n); //coincidentally identical to the C declaration. void main() { assert(factorial(3) == 6); } $ gcc -c factorial.c -ofactorial.o $ dmd blah.d factorial.o $ ./blah or $ gcc -c factorial.c -ofactorial.o $ ar rcs libfactorial.a factorial.o $ dmd blah.d -L-lfactorial $ ./blah Basically, you just translate the header files from C to D, then link to the C implementation. See http://code.dlang.org/packages/dstep for automatic translation of headers.
This is great. How practical (reliable ) is it to translate a large and complex header file like oci.h ( the interface for Oracle's database API ) to D?
You can do a lot of it by simply doing a find and replace in the file. For example, all C definitions of: unsigned char x become: ubyte x So a find an replace will do that for you quite easily. Other things like structs and typedefs are a bit more difficult to do with a find & replace. All the info you need is here anyway: wiki.dlang.org/Bind_D_to_C
Jun 11 2014
parent reply "Colin" <grogan.colin gmail.com> writes:
On Wednesday, 11 June 2014 at 15:14:19 UTC, Colin wrote:
 On Wednesday, 11 June 2014 at 14:28:49 UTC, belkin wrote:
 On Wednesday, 11 June 2014 at 14:02:08 UTC, John Colvin wrote:
 On Wednesday, 11 June 2014 at 13:52:09 UTC, belkin wrote:
 Example: I have this C function that is compiled into a 
 library

 //File: factorial.h
 int factorial(int n);


 //File: factorial.c
 #include "factorial.h"

 int factorial(int n)
 {
  if(n!=1)
   return n*factorial(n-1);
 }

 Question: How do I use it from D?
//File: blah.d extern(C) int factorial(int n); //coincidentally identical to the C declaration. void main() { assert(factorial(3) == 6); } $ gcc -c factorial.c -ofactorial.o $ dmd blah.d factorial.o $ ./blah or $ gcc -c factorial.c -ofactorial.o $ ar rcs libfactorial.a factorial.o $ dmd blah.d -L-lfactorial $ ./blah Basically, you just translate the header files from C to D, then link to the C implementation. See http://code.dlang.org/packages/dstep for automatic translation of headers.
This is great. How practical (reliable ) is it to translate a large and complex header file like oci.h ( the interface for Oracle's database API ) to D?
You can do a lot of it by simply doing a find and replace in the file. For example, all C definitions of: unsigned char x become: ubyte x So a find an replace will do that for you quite easily. Other things like structs and typedefs are a bit more difficult to do with a find & replace. All the info you need is here anyway: wiki.dlang.org/Bind_D_to_C
And here: http://dlang.org/interfaceToC.html
Jun 11 2014
parent Mike Parker <aldacron gmail.com> writes:
On 6/12/2014 12:17 AM, Colin wrote:

 So a find an replace will do that for you quite easily.
 Other things like structs and typedefs are a bit more difficult
 to do with a find & replace.
 All the info you need is here anyway:
 wiki.dlang.org/Bind_D_to_C
And here: http://dlang.org/interfaceToC.html
And: http://www.gamedev.net/page/resources/_/technical/game-programming/binding-d-to-c-r3122
Jun 12 2014
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 11 June 2014 at 14:28:49 UTC, belkin wrote:
 On Wednesday, 11 June 2014 at 14:02:08 UTC, John Colvin wrote:
 On Wednesday, 11 June 2014 at 13:52:09 UTC, belkin wrote:
 Example: I have this C function that is compiled into a 
 library

 //File: factorial.h
 int factorial(int n);


 //File: factorial.c
 #include "factorial.h"

 int factorial(int n)
 {
   if(n!=1)
    return n*factorial(n-1);
 }

 Question: How do I use it from D?
//File: blah.d extern(C) int factorial(int n); //coincidentally identical to the C declaration. void main() { assert(factorial(3) == 6); } $ gcc -c factorial.c -ofactorial.o $ dmd blah.d factorial.o $ ./blah or $ gcc -c factorial.c -ofactorial.o $ ar rcs libfactorial.a factorial.o $ dmd blah.d -L-lfactorial $ ./blah Basically, you just translate the header files from C to D, then link to the C implementation. See http://code.dlang.org/packages/dstep for automatic translation of headers.
This is great. How practical (reliable ) is it to translate a large and complex header file like oci.h ( the interface for Oracle's database API ) to D?
By hand it's just laborious, but it's very simple. Using dstep: It doesn't handle any pre-processor stuff, so a lot of complicated headers are out of the question. One approach is to run the pre-processor in gcc over the file, use dstep on the output, then reconstruct the conditional compilation stuff manually.
Jun 11 2014