Foxy
Foxy

Reputation: 1099

How to link a lib written in D to use it with a program written in C, under Windows, using MinGW GCC?

I would like to use a library written in D for a C program compilable with MinGW GCC, for Windows. Here are the codes:

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;
}

There is a tutorial on D's site, but it seems to target only Linux. Indeed, no explanation is given for creating such a dynamic D library for Windows and MinGW users.

D's documentation also says that the -shared option should generate a DLL version of the D code, but in my case, it generates an executable, I don't know why.

Also, anything that seems to generate files to be linked targets MVSC formats and nothing seems to be suitable for MinGW GCC compilers.

So, how can I generate a "GCC-friend" DLL with D, so that I can link it to my C program without having to use another compiler, such as GDC or LDC, via gcc main.c -o main -ldll -L. (I guess)?

Upvotes: 0

Views: 89

Answers (1)

Quark66
Quark66

Reputation: 29

I attached link with short explanation. Link D onto C is not so straightforward as C to D. Check D.org page here: https://dlang.org/spec/betterc.html

Upvotes: 0

Related Questions