【发布时间】:2017-06-16 13:08:49
【问题描述】:
我正在尝试为我的 python/numpy 代码编写一个简单的 c++ 扩展。但是,我无法编译扩展脚本,因为函数输入中的 PyArrayObject 没有成员。在我看来,我正在做与例如this post 相同的事情,但我想我错过了一些东西。这是一个无法编译的示例,因为我尝试检索维度成员:
#include <Python.h>
#include <stdio.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy/arrayobject.h"
static PyObject *function(PyObject *self, PyObject *args) {
PyObject *input;
PyArrayObject *array;
if (!PyArg_ParseTuple(args, "O", &input))
return NULL;
array= (PyArrayObject *)
PyArray_ContiguousFromObject(input, NPY_DOUBLE, 2, 2);
long n=array->dimensions[1];
}
这是在 linux 系统和 windows 7 计算机上编译的,使用 MVS 14.0 c++ 编译器,所以问题似乎与平台无关。
Python 版本:3.5
windows系统的异常输出:
paneltime/cfunctions.cpp(20): error C2039: 'dimensions': is not a member of 'tagPyArrayObject'c:\anaconda3\lib\site-packages\numpy\core\include\numpy\ndarraytypes.h(692): note: see declaration of 'tagPyArrayObject'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
【问题讨论】:
-
也许需要
import_array? Link to docs
标签: python c++ numpy python-c-extension