"不是类型标识符" Cython中的错误

时间:2017-10-29 19:52:17

标签: python cython

我是Cython的新手,我试图让一个从Python调用C函数的测试项目工作:

TEST.CPP

void testFn(int arr[]);

void testFn(int arr[])
{
    arr[0] = 1;
    arr[1] = 2;
} 

caller.pyx

cdef extern from "test.cpp":
    void testFn(int arr[])

cpdef myTest(*arr):
    testFn(arr)

setup.caller.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

sourcefiles = ['caller.pyx']
ext_modules = [Extension("caller", sourcefiles)]

setup(
    name = 'test app',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

但是当我尝试构建项目时,我收到一个错误:

$ python setup.caller.py build_ext --inplace
running build_ext
cythoning caller.pyx to caller.c

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "test.cpp":
    void testFn(int arr[])

cpdef myTest(*arr):
     ^
------------------------------------------------------------

caller.pyx:4:6: 'myTest' is not a type identifier

1 个答案:

答案 0 :(得分:1)

就我而言,我不需要" cpdef"或者" cdef"对于第二个定义,通常" def"做了诀窍:

def myTest(int[:] arr):
    testFn(&arr[0])