小编Lia*_* An的帖子

将 python 和 numpy 嵌入到 C++ 时出现 import_array() 错误

我编写了一个简单的代码尝试在 C++ 中使用 numpy。\n我的操作系统是ubuntu16.04gcc5.4.0Python2.7.12numpy1.15.0。\n这是我的代码test2.cpp

\n\n
#include "Python.h"\n#include "numpy/arrayobject.h"\n\nint main(int argc, char **argv) \n{\n    Py_Initialize();     \n    import_array(); \n\n    Py_Finalize(); \n    return 0; \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我用CMakeLists.txt这样的:

\n\n
cmake_minimum_required(VERSION 3.10) \n\nproject(test_python LANGUAGES CXX)\n\nset(CMAKE_CXX_STANDARD 11)\nset(CMAKE_BUILD_TYPE DEBUG)\n\nset(PYTHON_INCLUDE_PATH /usr/include/python2.7)\nset(PYTHON_LIBRARY /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so)\nset(NUMPY_INCLUDE_PATH /usr/local/lib/python2.7/dist-packages/numpy/core/include)\n\ninclude_directories(${PYTHON_INCLUDE_PATH})\ninclude_directories(${NUMPY_INCLUDE_PATH})\n\nadd_executable(test_python test2.cpp) \ntarget_link_libraries(test_python \n    ${PYTHON_LIBRARY}\n)\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是当我制作它时,我遇到了以下编译错误:

\n\n
/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 \xe2\x80\x98int\xe2\x80\x99 [-fpermissive]\n #define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } …
Run Code Online (Sandbox Code Playgroud)

c++ import numpy python-2.7

6
推荐指数
1
解决办法
2932
查看次数

标签 统计

c++ ×1

import ×1

numpy ×1

python-2.7 ×1