Создание расширения C ++ для Python 2.7.14 с помощью gcc (Windows) - PullRequest
0 голосов
/ 02 октября 2019

Я хочу создать расширение C ++ для Python. Для практики я решил начать с определения простой функции, которая добавляет два числа.

файл fns.cpp

#define _hypot hypot
#include "c:\Python27\include\Python.h"


static PyObject *add(PyObject *self, PyObject *args)
{
    float a;
    float b;
    PyArg_ParseTuple(args, "dd", &a, &b);
    return PyFloat_FromDouble(a + b);
};

static PyMethodDef methods[] =
{
    { "add", (PyCFunction)add, METH_O, nullptr },
    { nullptr, nullptr, 0, nullptr }

};

static struct PyModuleDef functions = 
{
    PyObject_HEAD_INIT,
    "functions",
    "fast add",
    -1,
    methods
};

PyMODINIT_FUNC PyInit_add(void)
{
    PyModule_Check(&functions);
}

также, чтобы построить расширение, я использую следующий код Python (файл setup.py):

from distutils.core import setup, Extension


module = Extension("fns",
                   include_dirs = ['.'],
                   sources = ['fns.cpp'])

setup(name='test', 
      ext_modules=[module])

построить с помощью команды

c:\Python27\python.exe setup.py build -c mingw32

, но у меня есть следующий вывод

running build
running build_ext
building 'fns' extension
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
c:\MinGW\bin\gcc.exe -mdll -O -Wall -I. -Ic:\Python27\include -Ic:\Python27\PC -c fns.cpp -o build\temp.win32-2.7\Release\fns.o
fns.cpp:20:27: error: variable 'PyModuleDef functions' has initializer but incomplete type
 static struct PyModuleDef functions =
                           ^~~~~~~~~
fns.cpp:22:5: error: 'PyObject_HEAD_INIT' was not declared in this scope
     PyObject_HEAD_INIT,
     ^~~~~~~~~~~~~~~~~~
error: command 'c:\\MinGW\\bin\\gcc.exe' failed with exit status 1

На самом деле, не понимаю,что я могу сделать с этими неприятностями.

...