Writing a D (D2) binding for existing C libraries
Asked Answered
S

2

7

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 conventions 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!

Scrapbook answered 4/9, 2010 at 15:50 Comment(7)
Yes, Windows (x86). Though I aim to make things portable if possible...Scrapbook
Out of curiosity, what libraries are you looking to port?Cleary
Either I get Derelict to work for D2, or I write parts myself. SDL / SFML, OpenGL, Lua, ENet, ... those are the ones I need most often. If I don't get it to work through some existing library, I'll try and write it myself, or bundle them. It's very depressing that many libs / ports are either for D1 or are abandoned ...; are you interested in IM contact? Would be nice to have a D buddy.Scrapbook
Derelict should work for D2 fine -- I'm using it myself right now. I know for sure that it works for Windows (XP and Win7) using DMD 2.048 and 2.047. I didn't manage to get it to work for Mac yet, but you said you're using Windows... btw, I don't use IM (I find it too distracting) but you can usually find me on the D news groups (digitalmars.com/NewsGroup.html).Cleary
I used DSSS to retrieve Derelict, does it automatically get the D2 version or do I need to add some option? When using Derelict in D2 I'm getting compiler errors from the Derelict sources. Works fine in D1 though. Derelict for D2 would be a great relieve...Scrapbook
I haven't tried using DSSS. When I got it, I first downloaded DMD manually (well, I already had it) then I simply downloaded the Derelict source from their repo and ran win32.mak using make -fwin32.mak from the command line. (See here: dsource.org/forums/viewtopic.php?t=4934). It just worked after that with no problems.Cleary
There exists bindings for Lua already: code.google.com/p/dluawrapperNerta
B
4

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.

Birefringence answered 4/9, 2010 at 16:21 Comment(2)
Thank you. I think the interfacing is pretty clear to me now. So, one way would be to make a project that consists of the interface files and OMF object files from C, and then compile these to one .lib? The .lib would be linked to, and the interface files are 'imported'. It would be difficult to distribute though (recompilation for most users?), but pretty easy to use.Scrapbook
druntime moved to github, new link. Also IIUC, there is consolidated C bindings project for D nowEarl
V
1

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

Volleyball answered 10/9, 2010 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.