On 02/29/2016 02:40 PM, Chris Katko wrote:

I want
to link to piece of D code at run-time. I want my D program to load,
scan for files which are whatever D-equivalent of a DLL/SO is, and load
those as well. Calling library functions, and having them call my core
functions.

It's the same as in C. Assuming that you have a ./libxyz.so that contains library_function() of type 'int library_func(int)', the following works:

import std.stdio;
import core.sys.linux.dlfcn;

extern(C) int library_func(int);
alias FuncType = int function(int);

int main()
{
    void * lib = dlopen("./libxyz.so", RTLD_NOW);

    if (!lib) {
        stderr.writefln("Failed to open library");
        return 1;
    }

    auto func = cast(FuncType)dlsym(lib, "library_func");

    if (!func) {
        stderr.writefln("Failed to find function");
        return 1;
    }

    writefln("Result: %s", func(10));

    return 0;
}

Ali

Reply via email to