May 24, 2018
I want to call some of my D code from Python. I'm annotating the pertinent functions with extern(C) export, as in

module foo;
extern(C) export int initialize() {
    return 42;
}

I compile with:

dmd -fPIC -shared ./foo.d

From the Python end, I can load the library using ctypes and the call works fine. The problem is, as soon as I have some state in my D module, as in:

module foo;

int call_count;

extern(C) export int initialize() {
    ++call_count;
    return 42;
}

the call to initialize from python gives:

AttributeError: ./foo.so: undefined symbol: initialize

Can you share some tips/examples of non-trivial/stateful D modules that are successfully export(C)ed and maybe consumed with ctypes? Are there any specific attributes that I need to annotate my module globals with?

Thank you!
Arredondo.