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

Lia*_* An 6 c++ import numpy python-2.7

我编写了一个简单的代码尝试在 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; } }\n                                                                                                                                                ^\n/home/camsys/projects/hmr_c/test/test2.cpp:7:5: note: in expansion of macro \xe2\x80\x98import_array\xe2\x80\x99\n     import_array(); \n     ^\n
Run Code Online (Sandbox Code Playgroud)\n\n

这很奇怪,因为当我使用Python3.5with时Numpy1.15.0,一切正常。谁能告诉我为什么会发生这个错误以及如何解决它

\n\n

我发现另一个类似的问题在 4 年前被问过,但没有答案Passing C++ array to python。这个问题是关于python3.4,而我正在处理python2.7

\n

小智 2

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; 
}
Run Code Online (Sandbox Code Playgroud)

现在,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
Run Code Online (Sandbox Code Playgroud)

int main函数应该返回一个整数,但在扩展内部(如果满足if statement条件),它返回python 版本 >= 3,因此它可以工作。对于 python 版本 < 3,该函数不返回任何内容,因此会出现错误。_import_array() < 0NULL = 0 (#define NULL 0)main

解决方法(对于 python 版本 < 3):

void temp_func() {
    import_array();
}

int main(int argc, char **argv) {
    Py_Initialize();     
    temp_func();

    Py_Finalize(); 
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

希望这能回答问题。