gpt4 book ai didi

python - python 的 C 扩展编译失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:25 25 4
gpt4 key购买 nike

我是为 python 创建 C 扩展模块的新手

我正在从 Mark Lutz 的“programming python”一书中寻求帮助

我已经使用这本书编写了一个代码来为 python 创建一个扩展模块,但是我在运行设置时遇到了一个错误

代码是

#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);
}
//___________________________________________________________________________

setup.py 的代码是

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])

我在使用“python setup.py build”命令时收到的错误消息是

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

最佳答案

python2和python3的兼容代码:

#include <Python.h>

#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif

// 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);
}
}

// registration table
static PyMethodDef hello_methods[]={
{"message", message, METH_VARARGS, "func doc"},
{NULL, NULL, 0, NULL}
};


#ifdef PY3K
// module definition structure for python3
static struct PyModuleDef hellomodule = {
PyModuleDef_HEAD_INIT,
"hello",
"mod doc",
-1,
hello_methods
};
// module initializer for python3
PyMODINIT_FUNC PyInit_hello()
{
return PyModule_Create(&hellomodule);
}
#else
// module initializer for python2
PyMODINIT_FUNC inithello() {
Py_InitModule3("hello", hello_methods, "mod doc");
}
#endif

关于python - python 的 C 扩展编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com