Using D within a rust codebase

Paul Backus snarwin at
Mon Jul 27 01:03:11 UTC 2020

On Sunday, 26 July 2020 at 21:18:19 UTC, powerboat9 wrote:
> I have an existing rust project, and I'm trying to rewrite part 
> of it in D. However, I'm not sure how to get rust -> dlang 
> interop working. I've looked into rust -> c -> dlang interop, 
> but I'm not sure how to get c -> dlang interop working either.

Here's the basic approach:

1. Write an `extern(C)` function in D.
2. Write a corresponding declaration of that function in C 
(typically, in a header file).
3. Use your D and C compilers to separately compile your D and C 
source files, respectively.
4. Use your D compiler to link the resulting object files.

For example, given these source files:

--- lib.d
extern(C) void hello()
	import std.stdio: writeln;
	writeln("Hello from D!");

--- lib.h
void hello();

--- main.c
#include "lib.h"

void main()
} can compile them with the following commands:

$ dmd -c lib.d     # produces lib.o
$ gcc -c main.c    # produces main.o
$ dmd main.o lib.o # produces main

You should now be able run `./main` and see it print "Hello from 

More information about the Digitalmars-d-learn mailing list