Using D within a rust codebase

Ali Çehreli acehreli at yahoo.com
Mon Jul 27 11:43:02 UTC 2020


On 7/27/20 3:50 AM, Jacob Carlborg wrote:> On 2020-07-27 03:03, Paul 
Backus wrote:
 >
 >> extern(C) void hello()
 >> {
 >>      import std.stdio: writeln;
 >>      writeln("Hello from D!");
 >> }
 >
 > The D runtime needs to be initialized first [1]. Then it should be
 > terminated as well [2].
 >
 > [1] https://dlang.org/phobos/core_runtime.html#.rt_init
 > [2] https://dlang.org/phobos/core_runtime.html#.rt_term
 >

They should be taken care of when the program is linked with a D compiler.

Otherwise, e.g. when the extern(C) code is part of a library written in 
D, one way of doing it is to add the following two functions to the 
library. (Typing by hand; may have typos).

import core.runtime;

pragma (crt_constructor)
extern (C)
int lib_init() {
   return Runtime.initialize() ? 0 : 1;
}

pragma (crt_destructor)
extern (C)
int lib_deinit() {
   return Runtime.terminate() ? 0 : 1;
}

Ali



More information about the Digitalmars-d-learn mailing list