digitalmars.D.learn - Create C header files from
- MaxNaderer (12/12) Feb 16 Hello guys,
- Lance Bachmeier (10/22) Feb 16 ```
- Lance Bachmeier (4/31) Feb 16 Oh sorry. I think you mean you are compiling D code to be called
- MaxNaderer (9/44) Feb 17 Thank you for your answer. Yes I am going the other way. I create
- Dennis (44/47) Feb 17 You can use traits, I've done something like that for Pascal
Hello guys,
so how do we generate C header files automatically for a library
compiled with dmd, ldc
in betterC. Like basic example below. I found the "-HC" flag
which can be used to output C++ header files. Do I need to
convert those manually to C?
```D
export extern(C) {
uint add(uint a, uint b) {
return a + b;
}
}
Feb 16
On Monday, 16 February 2026 at 17:07:53 UTC, MaxNaderer wrote:
Hello guys,
so how do we generate C header files automatically for a
library compiled with dmd, ldc
in betterC. Like basic example below. I found the "-HC" flag
which can be used to output C++ header files. Do I need to
convert those manually to C?
```D
export extern(C) {
uint add(uint a, uint b) {
return a + b;
}
}
```
dmd -c mycode.c -Hf=mycode.di
```
https://dlang.org/spec/importc.html#ctod
If you want to output the function bodies as D code, add
`-inline`:
```
dmd -c mycode.c -inline -Hf=mycode.di
```
Feb 16
On Monday, 16 February 2026 at 20:04:36 UTC, Lance Bachmeier wrote:On Monday, 16 February 2026 at 17:07:53 UTC, MaxNaderer wrote:Oh sorry. I think you mean you are compiling D code to be called from C.Hello guys, so how do we generate C header files automatically for a library compiled with dmd, ldc in betterC. Like basic example below. I found the "-HC" flag which can be used to output C++ header files. Do I need to convert those manually to C? ```D export extern(C) { uint add(uint a, uint b) { return a + b; } }``` dmd -c mycode.c -Hf=mycode.di ``` https://dlang.org/spec/importc.html#ctod If you want to output the function bodies as D code, add `-inline`: ``` dmd -c mycode.c -inline -Hf=mycode.di ```
Feb 16
On Monday, 16 February 2026 at 20:06:52 UTC, Lance Bachmeier wrote:On Monday, 16 February 2026 at 20:04:36 UTC, Lance Bachmeier wrote:Thank you for your answer. Yes I am going the other way. I create the lib in D and want to create a C header and other bindings. It should be a lib usable from many other languages like raylib but with the power of D! I have looked into __traits and I guess i need to go this way. The perfect way would be to get a XML or json from the functions so that the bindings can be easily automatedOn Monday, 16 February 2026 at 17:07:53 UTC, MaxNaderer wrote:Oh sorry. I think you mean you are compiling D code to be called from C.Hello guys, so how do we generate C header files automatically for a library compiled with dmd, ldc in betterC. Like basic example below. I found the "-HC" flag which can be used to output C++ header files. Do I need to convert those manually to C? ```D export extern(C) { uint add(uint a, uint b) { return a + b; } }``` dmd -c mycode.c -Hf=mycode.di ``` https://dlang.org/spec/importc.html#ctod If you want to output the function bodies as D code, add `-inline`: ``` dmd -c mycode.c -inline -Hf=mycode.di ```
Feb 17
On Tuesday, 17 February 2026 at 15:27:20 UTC, MaxNaderer wrote:I have looked into __traits and I guess i need to go this way.You can use traits, I've done something like that for Pascal instead of C. It roughly looks like: ```D string headerFromDModules(modules...)() { string result = "#pragma once\n"; foreach (mod; modules) { foreach (mem; __traits(allMembers, mod)) { static if (is(typeof(__traits(getMember, mod, mem)) == function)) { alias F = __traits(getMember, mod, mem); static if (functionLinkage!F == "C") { result ~= functionToHeader!(mem, F); } } } } result ~= "\n"; return result; } ``` `string functionToHeader(string mem, alias F)()` then has to introspect Parameters!F, ParameterIdentifierTuple!F, ParameterStorageClassTuple!F, and ReturnType!F and generate a C string accordingly. It can be used like so: ```D import pkg.a; import pkg.b; import std; void main() { File("header.h", "w").write(headerFromDModules!(pkg.a, pkg.b); } ```The perfect way would be to get a XML or json from the functions so that the bindings can be easily automateddmd has the `-X` flag to generate JSON which I'd wager has sufficient information to generate C headers, but you can also open an enhancement request in the issue tracker (https://github.com/dlang/dmd/issues) to make dmd directly generate C headers because it sounds like a worthwhile feature.
Feb 17








Dennis <dkorpel gmail.com>