Dynamic Library Support for D

Dicebot public at dicebot.lv
Thu Jan 2 11:33:37 PST 2014


It looks actually pretty capable with current 2.064.2 to me:

[dicebot at fog ~]$ cat lib.d
void foo(int x)
{
     import std.stdio;
     writefln("got %s in shared lib", x);
}
[dicebot at fog ~]$ cat test.d
import lib : foo;
import core.runtime : Runtime;
import core.sys.posix.dlfcn : dlsym;
import std.exception : enforce;

void main()
{
     auto handler = Runtime.loadLibrary("./lib.so");
     scope(exit) Runtime.unloadLibrary(handler);
     enforce(handler);
     auto func = cast(void function(int)) dlsym(handler, 
foo.mangleof.ptr);
     enforce(func);
     func(42);
}
[dicebot at fog ~]$ dmd -shared -fPIC -defaultlib=libphobos2.so lib.d
[dicebot at fog ~]$ dmd -L-ldl -defaultlib=libphobos2.so -run test.d
got 42 in shared lib


More information about the Digitalmars-d mailing list