开发者

Writing a D (D2) binding for existing C libraries

I'd really like to get more into D, but the lack of good library support is really hindering me. Therefore I'd like to create some D bindings for existing C libraries I'd like to use. I've never done any binding, but it doesn't look too difficult either.

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

  • What con开发者_运维技巧ventions should be used (I noticed version statements, aliases and regular constants / function definitions)?
  • What would be the difference between binding to a static library (and thus linked against) or a dynamic library? Is there any difference in the binding?
  • For binding a static library, the DMD compiler doesn't seem to accept .a or .o files, only .lib and .obj. Does this mean the libraries must be compiled with 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 a binding would be accomplished, I would be great full. Currently I can compile C code with DMC, link the object files and run functions from the C code in D. However, most C libraries just need a header file inclusion AND need to be linked against in C. I'm uncertain how to make bindings that work for that...

Thanks!


A few things to note:

  1. DMD and its linker Optlink work with the older OMF object file format, not COFF. This means that the C files you link against need to also be OMF. If you don't want to use DMC, there are tools that will convert COFF to OMF, though I don't know the details about them.

  2. As far as translating .h files to .d files, a utility called htod is packaged with DMD, and will do this translation for you, albeit somewhat imperfectly if you severely abuse the preprocessor. Generally, you use const, immutable, or enum for manifest constants, version statements for conditional compilation, and regular (possibly templated) functions for macro functions.

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


You may have a look at how Aldacron does with Derelict2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜