- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
操作系统:macOS Sierra 10.12.4
python 发行版:Anaconda python 3.6
我正在学习如何使用 distutils 将 numpy 数组传递给 c++。
运行时出现错误:
$ python setup.py build_ext
错误:
sample_wrap.cpp:4571:3: error: use of undeclared identifier 'import_array'
import_array();
^
1 error generated.
文件:sample.i
/* file: sample.i */
%module sample
%{
/* include C++ header files necessary to compile the interface */
#include "src/sample.h"
%}
%include "typemaps.i"
%include "src/numpy.i"
%init %{
import_array();
%}
%apply (int DIM1, double* IN_ARRAY1) {(int n, double *a), (int m, double *b)};
%apply (int DIM1, double* ARGOUT_ARRAY1) {(int size, double *arr)};
%include "src/sample.h"
文件:setup.py
# ----- file: setup.py -----
from distutils.core import setup, Extension
import numpy
import os
name = "sample" # name of the module
version = "1.0" # the module's version number
os.environ['CC'] = 'g++';
os.environ['CXX'] = 'g++';
setup(name=name, version=version,
ext_modules=[Extension(name='_sample',
sources=["sample.i", "src/sample.cpp"],
include_dirs=['src',numpy.get_include()],
swig_opts=["-c++"]
)]
)
文件:src/sample.cpp
/* ----- file: src/sample.cpp ----- */
#include <cmath>
#include "sample.h"
double dot(int n, double *a, int m, double *b){
double sum = 0.0;
for (int i=0; i<n; ++i){
sum += a[i]*b[i];
}
return sum;
}
void arange(int size, double *arr){
for (int i=0; i<size; ++i)
arr[i] = i;
}
文件:src/sample.h
/* ----- file: src/sample.h ----- */
#ifndef SAMPLE_H_
#define SAMPLE_H_
double dot(int n, double *a, int m, double *b);
void arange(int size, double *arr);
#endif // SAMPLE_H_
我尝试将 os.environ['CC'] = 'g++'
和 os.environ['CXX'] = 'g++'
更改为 os.environ['CC'] = 'g++-6'
和 os.environ['CXX'] = 'g++-6'
在 setup.py
,为了用 GUN g++ 编译而不是 clang,但仍然得到类似的错误:
sample_wrap.cpp: In function 'PyObject* PyInit__sample()':
sample_wrap.cpp:4571:16: error: 'import_array' was not declared in this scope
import_array();
^
最佳答案
我会尝试添加 #define SWIG_FILE_WITH_INIT
基于这个 documentation
/* file: sample.i */
%module sample
%{
#define SWIG_FILE_WITH_INIT
#include "src/sample.h"
%}
关于python - swig c++ 到 python (with numpy) : error: use of undeclared identifier 'import_array' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750318/
我在正确初始化 NumPy C API 时遇到问题。我想我已经将问题隔离到从不同的翻译单元调用 import_array,但我不知道为什么这很重要。 最小工作示例: header1.hpp #ifnd
我正在尝试编译一个 C++ 模块以在 scipy.weave 中使用,它由几个头文件和源 C++ 文件组成。这些文件包含广泛使用 Numpy/C-API 接口(interface)的类和方法。但我没能
我正在尝试在嵌入式 Python 中使用 Numpy。我在 Boost 1.57 中使用 Python 3.4 和 boost::python。为了防止 Python 设置一个信号处理程序来阻止我使用
我正在尝试将 C++ 中的 C 样式数组转换为 numpy 数组,但在尝试使用“PyArray_SimpleNewFromData”函数时遇到了问题。原来我需要打电话 import_array() 虽
//testNumpy.c #include #include #include int main(){ printf("import_array\n");fflush(stdout);
任何人都知道为什么我会收到此错误: Error in sys.excepthook: Traceback (most recent call last): method tm_excepthook
我写了一个简单的代码,试图在 C++ 中使用 numpy。 我的操作系统是 ubuntu16.04 , 与 gcc5.4.0 , Python2.7.12和 numpy1.15.0 . 这是我的代码
操作系统:macOS Sierra 10.12.4 python 发行版:Anaconda python 3.6 我正在学习如何使用 distutils 将 numpy 数组传递给 c++。 运行时出
我是一名优秀的程序员,十分优秀!