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

2024-09-28 21:53:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

测试.cpp

void testFn(int arr[]);

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

呼叫者.pyx

^{pr2}$

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

Tags: frompyimportbuildmodulessetupcppext