我有一个C函数,该函数将一个函数作为输入。我希望能够从python调用它。最终目标是让输入函数在python中定义但在C中进行评估。此刻,我只是在尝试使一个玩具示例正常工作。所以这就是我所做的...
我有一个名为“ Test.h”的头文件。这里有一个函数可以简单地用两个参数对一个函数求值。
#include <stdio.h>
double cEval( double ( *f )( double, double ), double n, double m ){
double k1 = f( n, m );
return k1;
};
然后我编写了一个cython文件来包装我的函数。该文件称为“ Test.pyx”。
cdef extern from "Test.h":
double cEval( double ( *f )( double, double ), double n, double m )
def Eval( f, n, m ):
return cEval( f, n, m )
最后,我编写了安装文件。
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(ext_modules=cythonize('Test.pyx'))
然后我使用命令“ python setup.py build_ext --inplace”编译“ setup.py”,该命令返回以下错误:
Compiling Test.pyx because it changed.
[1/1] Cythonizing Test.pyx
/usr/lib64/python2.7/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /maybehome/jritchie/c_interaction_example/Fun_Input/Test.pyx
tree = Parsing.p_module(s, pxd, full_module_name)
Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "Test.h":
double cEval( double ( *f )( double, double ), double n, double m )
def Eval( f, n, m ):
return cEval( f, n, m ) ^
------------------------------------------------------------
Test.pyx:5:15: Cannot convert Python object to 'double (*)(double, double)'
Traceback (most recent call last):
File "setup.py", line 4, in <module>
setup(ext_modules=cythonize('Test.pyx'))
File "/usr/lib64/python2.7/site-packages/Cython/Build/Dependencies.py", line 1086, in cythonize
cythonize_one(*args)
File "/usr/lib64/python2.7/site-packages/Cython/Build/Dependencies.py", line 1209, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: Test.pyx
有人知道我在做什么错吗,我该如何解决?