0

我很困惑如何加载共享对象库函数并将其与 Cython 一起使用。我创建了一个 dlfnc.pxd 文件,如下所示:

#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

和一个测试文件如下

#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)

这导致以下屏幕输出错误 test.pyx:29:9: Calling non-function type 'void'

如果有人能就如何继续进行建议,我已经为此困惑了整整一个星期。

谢谢

4

1 回答 1

0

您需要将其void *转换为函数指针才能调用它。

您可以使用 Cython 函数指针强制转换来做到这一点:

(<void (*)(int, char*, char*, char*, long, char*, long, long, long, long)> setup_FOR)(nc, hfld, hfmix, hrf, ierr, herr, lhfld, lhfmix, lhrf, lherr)
于 2013-04-19T05:47:43.813 回答