- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个简单的代码,试图在 C++ 中使用 numpy。
我的操作系统是 ubuntu16.04
, 与 gcc5.4.0
, Python2.7.12
和 numpy1.15.0
.
这是我的代码 test2.cpp
:
#include "Python.h"
#include "numpy/arrayobject.h"
int main(int argc, char **argv)
{
Py_Initialize();
import_array();
Py_Finalize();
return 0;
}
CMakeLists.txt
像这样:
cmake_minimum_required(VERSION 3.10)
project(test_python LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE DEBUG)
set(PYTHON_INCLUDE_PATH /usr/include/python2.7)
set(PYTHON_LIBRARY /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so)
set(NUMPY_INCLUDE_PATH /usr/local/lib/python2.7/dist-packages/numpy/core/include)
include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${NUMPY_INCLUDE_PATH})
add_executable(test_python test2.cpp)
target_link_libraries(test_python
${PYTHON_LIBRARY}
)
/usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/__multiarray_api.h:1547:144: error: return-statement with no value, in function returning ‘int’ [-fpermissive]
#define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } }
^
/home/camsys/projects/hmr_c/test/test2.cpp:7:5: note: in expansion of macro ‘import_array’
import_array();
^
Python3.5
时与
Numpy1.15.0
, 一切都好。
谁能告诉我为什么会发生这个错误以及如何解决它 ?
python3.4
, 而我正在处理
python2.7
.
最佳答案
import_array()
是在 /usr/local/lib/pythonX.Y/dist-packages/numpy/core/include/numpy/__multiarray_api.h
中定义的宏.在代码预处理期间(编译前),这个宏的定义在main
中被扩展和替换。功能就像这样:
int main(int argc, char **argv)
{
Py_Initialize();
{
if (_import_array() < 0) {
PyErr_Print();
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
return NUMPY_IMPORT_ARRAY_RETVAL;
}
}
Py_Finalize();
return 0;
}
NUMPY_IMPORT_ARRAY_RETVAL
也是在同一个文件中定义的宏
__multiarray_api.h
.该宏定义为
NULL
对于python3及更高版本,否则什么都没有。
#if PY_VERSION_HEX >= 0x03000000
#define NUMPY_IMPORT_ARRAY_RETVAL NULL
#else
#define NUMPY_IMPORT_ARRAY_RETVAL
#endif
int main
函数应该返回一个整数,但在扩展
if statement
中(如果满足条件
_import_array() < 0
),则返回
NULL = 0 (#define NULL 0)
对于python版本> = 3,因此它有效。对于 python 版本 < 3,
main
函数不返回任何内容,因此出现错误。
void temp_func() {
import_array();
}
int main(int argc, char **argv) {
Py_Initialize();
temp_func();
Py_Finalize();
return 0;
}
关于c++ - import_array() 在将 python 和 numpy 嵌入到 C++ 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52074167/
我在正确初始化 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++。 运行时出
我是一名优秀的程序员,十分优秀!