开发者

How to use a C library from D?

Today I heard about the D programming and that it is compatible to C code. Nevertheless I haven't found any information on whether it is 开发者_运维问答possible to use C libraries like GTK or PortAudio from D? If it is possible, could you explain how to do this?


It is possible to call C libraries from D. What you need to do is to convert the C header files to D. For the most part this is pretty straightforward, and there is a hard-to-use command-line tool to help automate the process. It's never really worked for me on anything but toy examples, but it could be a good start to see the kind of transformations that need to be done. Just put a snippet you're having trouble translating into a header by itself and see what htod does with it.

The biggest problem you'll usually encounter is creative use of the C preprocessor. Some things can be turned into version() statements in D, but not all.

As for actually compiling and linking with the code, on unix-like platforms I think you can compile and link in the C code using GCC. On Windows you either have to compile the C files using DMC and link with DMD. Or you can compile the C code into a DLL using any compiler capable of that, and then to link with DMD you need to make a DMD-compatible import lib out of the DLL. This can be done using the implib tool found in the free Basic Utilities Package available from DigitalMars.

There are also a lot of these header translations have already been done. It's useful to browse the Bindings project of Dsource first, or ask on the digitalmars D newsgroups first before embarking on something big like translating GTK headers. A lot of popular libraries like GTK have already been wrapped (e.g. here: GTKD)


D code can be linked with C object files, and can interact with C dlls, but you'll need to generate a D module from the C header file you want to use. The official D website has a guide for doing that very thing.


Popular alternative is to load the library during the run-time. Here is an example how to load libpng and call a libpng function:

module libpngtest;

import std.stdio;
import core.sys.posix.dlfcn;

alias uint function() png_access_version_number_t;

int main() {
  auto lib = dlopen("libpng.so".ptr, RTLD_LAZY | RTLD_LOCAL);
  if (lib is null) {
    writeln("EEEK!");
    writeln(to!string(dlerror()));
    return -1;
  } else {
    writeln("WOOT!");
    auto png_access_version_number = cast(png_access_version_number_t)dlsym(lib, "png_access_version_number");
    writeln(png_access_version_number());
  }

  if (dlclose(lib) == 0) {
    return 0;
  } else {
    return -1;
  }
} // main() function

// compile: dmd libpngtest.d -L-ldl
// run:     ./libpngtest

Use the DPaste to test it: http://www.dpaste.dzfl.pl/917bc3fb


You need to write C bindings. This answer explain how.


Take a look at http://dsource.org There are many projects that might help you to get start with

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜