I am new to creating C extension modules for python
I am taking help from the book "programming python" by Mark Lutz
I have written a code to create an extention module for python using this book but I get a error while I run the setup
The code is
#include<Python.h>
#include<string.h>
//MODULE FUNCTIONS..........................................................
static PyObject* message(PyObject *self, PyObject *args)
{
char *fromPython, result[1024];
if(!PyArg_ParseTuple(args, "s", &fromPython))
{
return NULL;
}
else
{
strcpy(result, "Hello, ");
strcat(result, fromPython);
return Py_BuildValue("s", result);
}
}
//___________________________________________________________________________
//METHOD REGISTRATION TABLE..................................................
static PyMethodDef hello_methods[]={
// name &func fmt doc
{"message", message, METH_VARARGS, "print a message"},
{NULL, NULL, 0, NULL}
};
//___________________________________________________________________________
//MODULE DEFINITION STRUCTURE................................................
static struct PyModuleDef hellomodule={
PyModuleDef_HEAD_INIT,
"hello"//name of module
"print messages"//module doc
-1//size of pre interpreter module state, -1=in global vars
hello_methods//link to methods table
};
//___________________________________________________________________________
//MODULE INITIALIZER---------------------------------------------------------
PyMODINIT_FUNC PyInit_hello()
{
PyModule_Create(&hellomodule);
}
//___________________________________________________________________________
The code for setup.py is
from distutils.core import setup, Extension
module1=Extension('hello', include_dirs=['/usr/local/include'], libraries=['pthread'], sources=['hello.c'])
setup(name='hello', version='1.0', description='debesh', url='http://www.debeshmohanty.com', ext_modules=[module1])
The error message I get when I use the command 'python setup.py build' is
running build
running build_ext
building 'hello' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/local/include -I/usr/include/python2.7 -c hello.c -o build/temp.linux-x86_64-2.7/hello.o
hello.c:40:15: error: variable ‘hellomodule’ has initializer but incomplete type
static struct PyModuleDef hellomodule={
^
hello.c:41:2: error: ‘PyModuleDef_HEAD_INIT’ undeclared here (not in a function)
PyModuleDef_HEAD_INIT,
^
hello.c:41:2: warning: excess elements in struct initializer
hello.c:41:2: warning: (near initialization for ‘hellomodule’)
hello.c:45:2: warning: excess elements in struct initializer
hello_methods//link to methods table
^
hello.c:45:2: warning: (near initialization for ‘hellomodule’)
hello.c:45:2: error: expected ‘}’ before ‘hello_methods’
hello.c:55:16: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
PyMODINIT_FUNC PyInit_hello()
^
hello.c: In function ‘PyInit_hello’:
hello.c:57:2: warning: implicit declaration of function ‘PyModule_Create’ [-Wimplicit-function-declaration]
PyModule_Create(&hellomodule);
^
hello.c: At top level:
hello.c:28:20: warning: ‘hello_methods’ defined but not used [-Wunused-variable]
static PyMethodDef hello_methods[]={
^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Python.hstatic struct PyModuleDef hellomoduledefinition. "," is missing after each parameter.