Dynamic Library Support for D

Jeroen Bollen jbinero at gmail.com
Thu Jan 2 13:50:45 PST 2014


On Thursday, 2 January 2014 at 19:33:38 UTC, Dicebot wrote:
> 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

That's not cross-platform though.


More information about the Digitalmars-d mailing list