www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D modules referenced by C source?

reply Matt <webwraith fastmail.fm> writes:
Hi, I apologise if this has already been asked, but I haven't gone and DLed a
news reader yet, so Firefox keeps complaining every time I try to use the
search box.

I was planning to include some C source into a D project, and was happily
writing the D module for the "header" for the source, when I ran into a little
problem. I want to include some const values in the module, which made me
realize I don't know how to include the module in the C source. I can see how
it would work from the D side of the compilation, but how would I go about
making the const values visible to the C code? Do I just #include "d_module.d"?
Or is there a different way to do this? What would the compilation process look
like for this?

Thank you in advance for any help you can provide.
May 06 2009
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Matt,

 don't know how to include the module in the C source. I can see how it
 would work from the D side of the compilation, but how would I go
 about making the const values visible to the C code? Do I just
 #include "
 

The best approach might be to write a .h file with only the constants in it and then use htod to convert it into a .d header. Aside from that you would end up hand editing bith files.
May 06 2009
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
BCS wrote:
 Reply to Matt,
 
 don't know how to include the module in the C source. I can see how it
 would work from the D side of the compilation, but how would I go
 about making the const values visible to the C code? Do I just
 #include "

The best approach might be to write a .h file with only the constants in it and then use htod to convert it into a .d header. Aside from that you would end up hand editing bith files.

If the constants are in a syntax recognized by both C and D (e.x. they're just integers, or enums), you can write the .h and use D's mixin(import("constants.h")), which would prevent you from having to maintain two files. If you're using some types differently in each, or want to use #defines on the C side, etc., you could use CTFE to convert (there was a CTFE regex engine floating around somewhere a whiel back... Actually, I'd like to get my hands on that myself if anyone has the link).
May 06 2009
parent BCS <ao pathlink.com> writes:
Reply to Robert,

 mixin(import("constants.h"))
 

Darn, now I want to go out and build stuff to do this: mixin(h2d!(import("stdio.h")));
May 06 2009
prev sibling next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 07 May 2009 03:02:16 +0400, Matt <webwraith fastmail.fm> wrote:

 Hi, I apologise if this has already been asked, but I haven't gone and  
 DLed a news reader yet, so Firefox keeps complaining every time I try to  
 use the search box.

 I was planning to include some C source into a D project, and was  
 happily writing the D module for the "header" for the source, when I ran  
 into a little problem. I want to include some const values in the  
 module, which made me realize I don't know how to include the module in  
 the C source. I can see how it would work from the D side of the  
 compilation, but how would I go about making the const values visible to  
 the C code? Do I just #include "d_module.d"? Or is there a different way  
 to do this? What would the compilation process look like for this?

 Thank you in advance for any help you can provide.

Did you try htod? http://www.digitalmars.com/d/2.0/htod.html There is also BCD http://www.dsource.org/projects/bcd Try both tools and let us know if it helped.
May 06 2009
parent reply Matt <webwraith fastmail.fm> writes:
Denis Koroskin Wrote:

 On Thu, 07 May 2009 03:02:16 +0400, Matt <webwraith fastmail.fm> wrote:
 
 Hi, I apologise if this has already been asked, but I haven't gone and  
 DLed a news reader yet, so Firefox keeps complaining every time I try to  
 use the search box.

 I was planning to include some C source into a D project, and was  
 happily writing the D module for the "header" for the source, when I ran  
 into a little problem. I want to include some const values in the  
 module, which made me realize I don't know how to include the module in  
 the C source. I can see how it would work from the D side of the  
 compilation, but how would I go about making the const values visible to  
 the C code? Do I just #include "d_module.d"? Or is there a different way  
 to do this? What would the compilation process look like for this?

 Thank you in advance for any help you can provide.

Did you try htod? http://www.digitalmars.com/d/2.0/htod.html There is also BCD http://www.dsource.org/projects/bcd Try both tools and let us know if it helped.

Thanks, but the problem isn't with my writing the D module for C. It's how the C source imports that module to access the constants in it.
May 07 2009
parent Robert Fraser <fraserofthenight gmail.com> writes:
Matt wrote:
 Denis Koroskin Wrote:
 
 On Thu, 07 May 2009 03:02:16 +0400, Matt <webwraith fastmail.fm> wrote:

 Hi, I apologise if this has already been asked, but I haven't gone and  
 DLed a news reader yet, so Firefox keeps complaining every time I try to  
 use the search box.

 I was planning to include some C source into a D project, and was  
 happily writing the D module for the "header" for the source, when I ran  
 into a little problem. I want to include some const values in the  
 module, which made me realize I don't know how to include the module in  
 the C source. I can see how it would work from the D side of the  
 compilation, but how would I go about making the const values visible to  
 the C code? Do I just #include "d_module.d"? Or is there a different way  
 to do this? What would the compilation process look like for this?

 Thank you in advance for any help you can provide.

There is also BCD http://www.dsource.org/projects/bcd Try both tools and let us know if it helped.

Thanks, but the problem isn't with my writing the D module for C. It's how the C source imports that module to access the constants in it.

Well, if you write the constants in C, you can have them automatically translated to D in the build process and then just include the result.
May 07 2009
prev sibling parent Robert Clipsham <robert octarineparrot.com> writes:
Matt wrote:
 Hi, I apologise if this has already been asked, but I haven't gone and DLed a
news reader yet, so Firefox keeps complaining every time I try to use the
search box.
 
 I was planning to include some C source into a D project, and was happily
writing the D module for the "header" for the source, when I ran into a little
problem. I want to include some const values in the module, which made me
realize I don't know how to include the module in the C source. I can see how
it would work from the D side of the compilation, but how would I go about
making the const values visible to the C code? Do I just #include "d_module.d"?
Or is there a different way to do this? What would the compilation process look
like for this?
 
 Thank you in advance for any help you can provide.

I've never tried this, but I guess you'd do something like: extern(C) void myFunction(); In the D file, compile it, then link the resulting object in when you compile the C app. You would of course need to define this function and anything else you've extern(C)'d in the C app so it's available. I'm guessing that you wouldn't be able to use a large chunk of D's functionality without linking in a good chunk (if not all) of a D runtime too, but I may be wrong.
May 07 2009