How to use a C library from D?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 16:00:01

问题


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?


回答1:


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)




回答2:


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.




回答3:


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




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/1630597/how-to-use-a-c-library-from-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!