I'm trying to get a test project working that calls a C function, with an array parameter, from Python:
test.cpp:
void testFn(int arr[]);
void testFn(int arr[])
{
arr[0] = 1;
arr[1] = 2;
}
caller.pyx:
import ctypes
cdef extern from "test.cpp":
void testFn(int arr[])
def myTest():
a = [0, 0]
arr = a.ctypes.data_as(ctypes.POINTER(ctypes.c_integer))
testFn(arr)
print(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
)
But when I try to build the project I get an error:
$ python setup.caller.py build_ext --inplace
running build_ext
cythoning caller.pyx to caller.c
Error compiling Cython file:
------------------------------------------------------------
...
def myTest():
a = [0, 0]
arr = a.ctypes.data_as(ctypes.POINTER(ctypes.c_integer))
testFn(arr)
^
------------------------------------------------------------
caller.pyx:13:11: Cannot convert Python object to 'int *'
arra type.cdef int* arrshould do the trick.