0

I'm puzzling how to load a shared object library function and use this with Cython. I've created a dlfnc.pxd file as below:

#dlfcn.pxd
cdef extern from *:
    ctypedef char const_char "const char"

cdef extern from 'dlfcn.h' nogil:
    void* dlopen(const_char *filename, int flag)
    char *dlerror()
    void *dlsym(void *handle, const_char *symbol)
    int dlclose(void *handle)

    unsigned int RTLD_LAZY
    unsigned int RTLD_NOW
    unsigned int RTLD_GLOBAL
    unsigned int RTLD_LOCAL
    unsigned int RTLD_NODELETE
    unsigned int RTLD_NOLOAD
    unsigned int RTLD_DEEPBIND

    unsigned int RTLD_DEFAULT
    long unsigned int RTLD_NEXT

and a test file as below

#test.pyx
cimport dlfcn

#load shared object
cdef void *handle = dlfcn.dlopen("/usr/local/lib/librefprop.so",
                                 dlfcn.RTLD_NOW | dlfcn.RTLD_GLOBAL)
if handle == NULL:
    print dlfcn.dlerror()
    1/0 #raise error still need to implement exception.....

#load function
cdef void *setup_FOR = dlfcn.dlsym(handle, "setup0_")
if setup_FOR == NULL:
    print dlfcn.dlerror()
    1/0 #raise error and still need to implement exception

#some variables
cdef int nc = 2
cdef char *hfld = "/usr/local/lib/refprop/fluids/WATER.FLD|/usr/local/lib/refprop/fluids/AMMONIA.FLD|"
cdef char *hfmix = '/usr/local/lib/refprop/fluids/HMX.BNC'
cdef char *hrf = 'DEF'
cdef long ierr = 0
cdef char *herr = ''
cdef long lhfld = 10000
cdef long lhfmix = 255
cdef long lhrf = 3
cdef long lherr = 255

#call function
setup_FOR(nc, hfld, hfmix, hrf, ierr, herr, lhfld, lhfmix, lhrf, lherr)

this result in error with the following screen output test.pyx:29:9: Calling non-function type 'void'

I have been puzzling about this for a whole week now if anybody can advice on how to proceed that would be great.

Thanks

1 Answers1

0

You'll need to cast the void * into a function pointer in order to call it.

You can do so with a Cython function pointer cast:

(<void (*)(int, char*, char*, char*, long, char*, long, long, long, long)> setup_FOR)(nc, hfld, hfmix, hrf, ierr, herr, lhfld, lhfmix, lhrf, lherr)
nneonneo
  • 171,345
  • 36
  • 312
  • 383