为现有 C 库编写 D (D2) 绑定

发布于 2024-09-17 00:57:51 字数 494 浏览 6 评论 0原文

我真的很想更多地了解 D,但缺乏良好的库支持确实阻碍了我。因此,我想为我想要使用的现有 C 库创建一些 D 绑定。我从来没有做过任何绑定,但看起来也不是太难。

我计划为 D2 执行此操作(不是专门为 D1,但如果可以同时为两者,那就更好了)。我正在使用 DMD2 编译器。

  • 应该使用什么约定(我注意到版本声明、别名和常规常量/函数定义)?
  • 绑定到静态库(并因此链接)或动态库有什么区别?绑定有什么区别吗?
  • 对于绑定静态库,DMD编译器似乎不接受.a或.o文件,只接受.lib和.obj。这是否意味着库必须使用 DMC 编译器(而不是 GCC 编译器)进行编译,然后通过 DMD 编译器进行链接?

如果有人有一个关于如何完成绑定的非常简短的示例,我会非常满意。目前,我可以使用 DMC 编译 C 代码,链接目标文件并从 D 中的 C 代码运行函数。但是,大多数 C 库只需要包含头文件,并且需要在 C 中链接。我不确定如何制作有效的绑定...

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-09-24 00:57:51

有几点需要注意:

  1. DMD 及其链接器 Optlink 使用较旧的 OMF 目标文件格式,而不是 COFF。这意味着您链接的 C 文件也必须是 OMF。如果您不想使用 DMC,有一些工具可以将 COFF 转换为 OMF,但我不知道它们的详细信息。

  2. 就将 .h 文件转换为 .d 文件而言,DMD 中包含一个名为 htod 的实用程序,它将为您完成此转换,尽管如果您严重滥用预处理器,则可能会有些不完美。通常,您使用 constimmutableenum 作为清单常量,使用 version 语句进行条件编译,并使用常规宏函数的(可能是模板化的)函数。

就示例而言,一个值得一看的地方是在运行时, 其中包含整个 C 标准库的绑定。

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.

桃酥萝莉 2024-09-24 00:57:51

您可以看看 Aldacron 如何使用 Derelict2

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文