我很困惑如何加载共享对象库函数并将其与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:调用非函数类型'void'
如果有人可以提出如何继续进行的建议那么我现在整整一个星期一直都很困惑。
由于
答案 0 :(得分: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)