问题描述
对于Windows,我想使用用D编写的库编写与MinGW GCC兼容的C程序。这是代码:
dll.d
extern (C) int dsquare(int n) nothrow
{
return n * n;
}
main.c
#include <stdio.h>
int main()
{
int res = dsquare(6); // Expect '36'
printf("res = %d\n",res);
return 0;
}
有a tutorial on D's site,但它似乎仅针对Linux。确实,没有为Windows和MinGW用户创建这样的动态D库提供任何解释。
D's documentation也说-shared
选项应该生成D代码的DLL版本,但就我而言,它生成可执行文件,我不知道为什么。
此外,任何看起来会生成要链接的文件的东西都以MVSC格式为目标,而似乎没有任何东西适合MinGW GCC编译器。
因此,如何使用D生成“ GCC友好” DLL,这样我就可以将其链接到C程序,而不必通过gcc main.c -o main -ldll -L.
使用其他编译器,例如GDC或LDC(我猜)?
解决方法
我附上了简短说明的链接。将D链接到C并不像C链接到D那样简单。请在此处查看D.org页面: https://dlang.org/spec/betterc.html