digitalmars.D - ImportC + -H = bindings
- Kapendev (18/18) Apr 23 I recently made a script/library called
- Lance Bachmeier (8/27) Apr 23 I'm not sure how it compares to clonoa, but I let ImportC do the
- Kapendev (29/36) Apr 23 It does the same thing! And yeah, by default the `.di` files will
- Lance Bachmeier (5/41) Apr 24 This would be a good addition to the compiler if the two of us
- Serg Gini (3/7) Apr 24 I agree. This addition to the compiler functionality of ImportC
- jmh530 (3/10) Apr 24 If all the kinks get worked out and people start making a lot of
- Kapendev (5/17) Apr 24 Yeah, and I think that the compiler doesn't need to do everything
- Kapendev (14/18) Apr 24 Or maybe not. C is weird sometimes lol:
- Lance Bachmeier (7/19) Apr 24 The issue with this is that the compiler currently gives
- Serg Gini (2/4) Apr 24 Have you fixed all the bugs?
- Kapendev (5/9) Apr 24 There will be some, but most of the bad and easy to find ones are
I recently made a script/library called [Clonoa](https://github.com/Kapendev/clonoa) that generates D bindings from C files using ImportC. The way it works is really simple: 1. Runs: `compiler -o- -H file.c` 2. Parses the `.di` file from the command above and prints a cleaned version of it to stdout. The generated output tries to avoid some redefinitions of Libc symbols and it assumes that names starting with `_` are always private, so they get skipped. Overall it does a good job of including the right things. For example, I was able to create bindings for SDL2 and use them. To use the script via DUB, you can run: ``` dub run clonoa ``` Or even simpler, copy-paste the script into your own project. Clonoa is just one D file.
Apr 23
On Friday, 24 April 2026 at 01:09:28 UTC, Kapendev wrote:I recently made a script/library called [Clonoa](https://github.com/Kapendev/clonoa) that generates D bindings from C files using ImportC. The way it works is really simple: 1. Runs: `compiler -o- -H file.c` 2. Parses the `.di` file from the command above and prints a cleaned version of it to stdout. The generated output tries to avoid some redefinitions of Libc symbols and it assumes that names starting with `_` are always private, so they get skipped. Overall it does a good job of including the right things. For example, I was able to create bindings for SDL2 and use them. To use the script via DUB, you can run: ``` dub run clonoa ``` Or even simpler, copy-paste the script into your own project. Clonoa is just one D file.I'm not sure how it compares to clonoa, but I let ImportC do the translation of C headers in their entirety using the -Hf switch. Now, that isn't going to generate .di files that work, but I have a program that parses the files to remove duplicate definitions, functions with keywords as names, and whatever else doesn't work. It's at the point that it can automatically convert any C header I try.
Apr 23
On Friday, 24 April 2026 at 02:09:00 UTC, Lance Bachmeier wrote:I'm not sure how it compares to clonoa, but I let ImportC do the translation of C headers in their entirety using the -Hf switch. Now, that isn't going to generate .di files that work, but I have a program that parses the files to remove duplicate definitions, functions with keywords as names, and whatever else doesn't work. It's at the point that it can automatically convert any C header I try.It does the same thing! And yeah, by default the `.di` files will not always work because of D keywords or of some other weird translations like: ``` alias void foo_func_type(...) ``` The solution for this one is to change the order of things: name first and then the function type. In general you have to clean the `.di` files. Here are some things that I can control with my script (some options are not exposed in the CLI): ```d struct ClonoaArgs { string compiler = defaultCompiler; string headerPath; string moduleName; string[] headerIncludes; string[] headerPrefixes; string[] opaqueStructs; bool removeRepeatedEnums; string moduleSymbolHeader = defaultModuleSymbolHeader; string indentation = defaultIndentation; string[string] typeMap; string[] typeSkipList; string[] funcSkipList; string[] lineSkipList; } ```
Apr 23
On Friday, 24 April 2026 at 02:56:31 UTC, Kapendev wrote:On Friday, 24 April 2026 at 02:09:00 UTC, Lance Bachmeier wrote:This would be a good addition to the compiler if the two of us working independently were able to solve the problem. In an alternative world where it's worth one's time to propose such things, it would make a great addition to the compiler.I'm not sure how it compares to clonoa, but I let ImportC do the translation of C headers in their entirety using the -Hf switch. Now, that isn't going to generate .di files that work, but I have a program that parses the files to remove duplicate definitions, functions with keywords as names, and whatever else doesn't work. It's at the point that it can automatically convert any C header I try.It does the same thing! And yeah, by default the `.di` files will not always work because of D keywords or of some other weird translations like: ``` alias void foo_func_type(...) ``` The solution for this one is to change the order of things: name first and then the function type. In general you have to clean the `.di` files. Here are some things that I can control with my script (some options are not exposed in the CLI): ```d struct ClonoaArgs { string compiler = defaultCompiler; string headerPath; string moduleName; string[] headerIncludes; string[] headerPrefixes; string[] opaqueStructs; bool removeRepeatedEnums; string moduleSymbolHeader = defaultModuleSymbolHeader; string indentation = defaultIndentation; string[string] typeMap; string[] typeSkipList; string[] funcSkipList; string[] lineSkipList; } ```
Apr 24
On Friday, 24 April 2026 at 13:52:48 UTC, Lance Bachmeier wrote:This would be a good addition to the compiler if the two of us working independently were able to solve the problem. In an alternative world where it's worth one's time to propose such things, it would make a great addition to the compiler.I agree. This addition to the compiler functionality of ImportC could be useful.
Apr 24
On Friday, 24 April 2026 at 14:28:14 UTC, Serg Gini wrote:On Friday, 24 April 2026 at 13:52:48 UTC, Lance Bachmeier wrote:If all the kinks get worked out and people start making a lot of use of it, then it's an easier sell to get in compiler.This would be a good addition to the compiler if the two of us working independently were able to solve the problem. In an alternative world where it's worth one's time to propose such things, it would make a great addition to the compiler.I agree. This addition to the compiler functionality of ImportC could be useful.
Apr 24
On Friday, 24 April 2026 at 14:30:20 UTC, jmh530 wrote:On Friday, 24 April 2026 at 14:28:14 UTC, Serg Gini wrote:Yeah, and I think that the compiler doesn't need to do everything the script does. One QoL things would be to automatically change names like `alias` to `alias_` when using the `-H` flag so it's easier for people to modify the output.On Friday, 24 April 2026 at 13:52:48 UTC, Lance Bachmeier wrote:If all the kinks get worked out and people start making a lot of use of it, then it's an easier sell to get in compiler.This would be a good addition to the compiler if the two of us working independently were able to solve the problem. In an alternative world where it's worth one's time to propose such things, it would make a great addition to the compiler.I agree. This addition to the compiler functionality of ImportC could be useful.
Apr 24
On Friday, 24 April 2026 at 15:40:13 UTC, Kapendev wrote:Yeah, and I think that the compiler doesn't need to do everything the script does. One QoL things would be to automatically change names like `alias` to `alias_` when using the `-H` flag so it's easier for people to modify the output.Or maybe not. C is weird sometimes lol: ```c struct debug { int value; }; void debug(struct debug debug) { debug.value += 1; } void main() { struct debug d = {0}; debug(d); } ```
Apr 24
On Friday, 24 April 2026 at 14:30:20 UTC, jmh530 wrote:On Friday, 24 April 2026 at 14:28:14 UTC, Serg Gini wrote:The issue with this is that the compiler currently gives incorrect output that you cannot use without further transformation. It's not about adding a feature to the compiler, it's about fixing an existing feature that doesn't work. The truth is that it will be added if the right person makes the request, not if there is evidence that it is being used.On Friday, 24 April 2026 at 13:52:48 UTC, Lance Bachmeier wrote:If all the kinks get worked out and people start making a lot of use of it, then it's an easier sell to get in compiler.This would be a good addition to the compiler if the two of us working independently were able to solve the problem. In an alternative world where it's worth one's time to propose such things, it would make a great addition to the compiler.I agree. This addition to the compiler functionality of ImportC could be useful.
Apr 24
On Friday, 24 April 2026 at 01:09:28 UTC, Kapendev wrote:I recently made a script/library called [Clonoa](https://github.com/Kapendev/clonoa) that generates DHave you fixed all the bugs?
Apr 24
On Friday, 24 April 2026 at 09:54:55 UTC, Serg Gini wrote:On Friday, 24 April 2026 at 01:09:28 UTC, Kapendev wrote:There will be some, but most of the bad and easy to find ones are fixed. For example, the type map now doesn't replace the name `PROFILE` with `PROstdc.stdio.FILE` lol.I recently made a script/library called [Clonoa](https://github.com/Kapendev/clonoa) that generates DHave you fixed all the bugs?
Apr 24









Kapendev <alexandroskapretsos gmail.com> 