python编译安装没有c扩展_python的C扩展编译失败

在尝试根据马克·卢茨的《编程Python》创建C扩展模块时,遇到了编译错误。错误包括未声明的`PyModuleDef_HEAD_INIT`,结构初始化问题以及`PyModule_Create`函数的隐式声明。这阻碍了Python C扩展模块的安装过程。
摘要由CSDN通过智能技术生成

我不熟悉为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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值