www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Generating C Headers From D Code

reply Jack Stouffer <jack jackstouffer.com> writes:
I need to generate plain C99 .h files from a D module's extern(C) 
declarations, so that I can link a DMD generated .o file with a C 
code base. Are there any automated tools which do this?

I know the compiler has C++ header generation, and there's tons 
of tools which exist for importing C headers into D code. I'm not 
aware of anything which goes the other direction. Google wasn't 
much help either.
Aug 05 2021
next sibling parent reply Tejas <notrealemail gmail.com> writes:
On Thursday, 5 August 2021 at 16:28:35 UTC, Jack Stouffer wrote:
 I need to generate plain C99 .h files from a D module's 
 extern(C) declarations, so that I can link a DMD generated .o 
 file with a C code base. Are there any automated tools which do 
 this?

 I know the compiler has C++ header generation, and there's tons 
 of tools which exist for importing C headers into D code. I'm 
 not aware of anything which goes the other direction. Google 
 wasn't much help either.
I also can't find anything... until someone else comes with a better answer, maybe you can do this: Use the ```-H``` compiler flag to generate ```.di``` files. Remove all the ```extern(C)``` decls in the .di files. Rename the file extension from ```.di``` to ```.h``` Technically, it should work. Hopefully someone else knows better.
Aug 05 2021
next sibling parent Tejas <notrealemail gmail.com> writes:
On Thursday, 5 August 2021 at 17:02:33 UTC, Tejas wrote:
 On Thursday, 5 August 2021 at 16:28:35 UTC, Jack Stouffer wrote:
 [...]
I also can't find anything... until someone else comes with a better answer, maybe you can do this: Use the ```-H``` compiler flag to generate ```.di``` files. Remove all the ```extern(C)``` decls in the .di files. Rename the file extension from ```.di``` to ```.h``` Technically, it should work. Hopefully someone else knows better.
A small tutorial-ish page for d interface files: https://wiki.dlang.org/Compiling_and_linking_with_DMD_on_Windows#Writing_and_Using_D_Interface_Files
Aug 05 2021
prev sibling parent Jack Stouffer <jack jackstouffer.com> writes:
On Thursday, 5 August 2021 at 17:02:33 UTC, Tejas wrote:
 On Thursday, 5 August 2021 at 16:28:35 UTC, Jack Stouffer wrote:
 I need to generate plain C99 .h files from a D module's 
 extern(C) declarations, so that I can link a DMD generated .o 
 file with a C code base. Are there any automated tools which 
 do this?

 I know the compiler has C++ header generation, and there's 
 tons of tools which exist for importing C headers into D code. 
 I'm not aware of anything which goes the other direction. 
 Google wasn't much help either.
I also can't find anything... until someone else comes with a better answer, maybe you can do this: Use the ```-H``` compiler flag to generate ```.di``` files. Remove all the ```extern(C)``` decls in the .di files. Rename the file extension from ```.di``` to ```.h``` Technically, it should work. Hopefully someone else knows better.
Well, that's disappointing. I suppose everyone just makes there main file a D file when converting C projects so they don't have this problem. Eventually I'll have to write a script which takes .di files and generates .h files, but doing it manually will have to work for now.
Aug 06 2021
prev sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 5 August 2021 at 16:28:35 UTC, Jack Stouffer wrote:
 I know the compiler has C++ header generation
Your best bet is probably to use this and just do the slight modifications to turn it from C++ into C. dmd -HC followed by a modification script to rip some of the stuff out might get you somewhere. A long time ago i made a program called dtoh that did it from the generated json but like i think that broke years ago, i haven't touched it for ages. can't find it anymore. dmd -HC surely better anyway.
Aug 06 2021