我不熟悉为python创建C扩展模块
我从马克·卢茨的《编程python》一书中得到帮助
我用这本书编写了一段代码来为python创建一个扩展模块,但是在运行安装程序时遇到了一个错误
代码是#include
#include
//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);
}
//___________________________________________________________________________
代码设置.py是
^{pr2}$
当我使用命令python时得到的错误消息设置.py建造'是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