Writing a D (D2) Binding for Existing C Libraries

I would love to get more in D, but the lack of good library support really bothers me. Therefore, I would like to create some D bindings for existing C libraries that I would like to use. I have never done bindings, but it is also not too difficult.

I plan to do this for D2 (not specifically D1, but if it can be for both, even better). I am using the DMD2 compiler.

  • What conventions should be used (I noticed version statements, aliases and regular constants / function definitions)?
  • What is the difference between binding to a static library (and therefore related) or a dynamic library? Is there a difference in binding?
  • To bind a static library, the DMD compiler does not seem to accept .a or .o files, only .lib and .obj. Does this mean that libraries must be compiled using the DMC compiler (as opposed to the GCC compiler) and then linked through the DMD compiler?

If someone had a very short example of how the binding would be done, I would be very complete. Currently, I can compile C code with DMC, link object files and run functions from C code in D. However, most C libraries just need to include the header file and need to link it to C. I'm not sure how to make the bindings that work for this ...

Thanks!

+6
c binding d dmd
source share
2 answers

A few notes:

  • DMD and its Optlink linker work with the old OMF file, not COFF. This means that the C files you are referencing must also be OMF. If you do not want to use DMC, there are tools that convert COFF to OMF, although I do not know the details about them.

  • As for translating .h files to .d files, the htod utility will be packaged in DMD and will perform this translation for you, although it is somewhat imperfect if you seriously abuse the preprocessor. Typically, you use const , immutable or enum for manifest constants, version statements for conditional compilation, and regular (possibly template) functions for macro functions.

As for the examples, one place to look would be in druntime mode, which contains bindings for the entire C standard library.

+4
source share

You might see how Aldacron works with Derelict2 .

+1
source share

All Articles