我编写了一个简单的代码尝试在 C++ 中使用 numpy。\n我的操作系统是ubuntu16.04、gcc5.4.0、Python2.7.12和numpy1.15.0。\n这是我的代码test2.cpp:
#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}\nRun Code Online (Sandbox Code Playgroud)\n\n我用CMakeLists.txt这样的:
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)\nRun 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)