建立在GCC的Python(2.7)模块4.8失败 [英] Build Python (2.7) module on GCC 4.8 fails
本文介绍了建立在GCC的Python(2.7)模块4.8失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用C API构建一个Python模块/扩展写的,但它失败:
%python2 cmath.py建设
运行构建
运行build_ext
建设c_math'延伸
创建编译
创建构建/ temp.linux-x86_64-2.7
GCC -pthread -fno严格走样-march = X86-64 -mtune =通用-O2 -pipe -fstack保护器--param = SSP缓冲区大小= 4 -D_FORTIFY_SOURCE = 2 -DNDEBUG -march = X86-64 -mtune =通用-O2 -pipe -fstack保护器--param = SSP缓冲区大小= 4 -D_FORTIFY_SOURCE = 2 -fPIC -I / usr / include目录/ python2.7 -c c_math.c -o建立/温度。 Linux的x86_64-2.7 / c_math.o
c_math.c:18:5:警告:初始化从兼容的指针类型[默认启用]
{更多,c_math_more,METH_VARARGS,N + NN使用C API},
^
c_math.c:18:5:警告:(近初始化c_mathMethods [0] .ml_meth')[默认启用]
在文件从/usr/include/python2.7/Python.h:58:0包括,
从c_math.c:1:
/usr/include/python2.7/pyport.h:802:39:错误:预期','或';'之前无效
#定义PyMODINIT_FUNC无效
^
c_math.c:22:1:注意:宏'PyMODINIT_FUNC扩张
PyMODINIT_FUNC
^
错误:命令'GCC'失败,退出状态1
我试过手动建立一个没有的distutils,但我得到同样的错误/警告讯息
c_math.c:
的#include< Python.h>静态的PyObject *
c_math_more(*的PyObject自我,的PyObject * N,*的PyObject NN){
如果(PyInt_Check(正)及&放大器; PyInt_Check(NN)){
浮CN,CNN,结果; CN = PyInt_AsLong(N);
CNN = PyInt_AsLong(NN); 结果= CN + CNN; 返回PyInt_FromLong(结果);
}
}静态PyMethodDef c_mathMethods [] = {
{更多,c_math_more,METH_VARARGS,N + NN使用C API},
{NULL,NULL,0,NULL}
}PyMODINIT_FUNC
initc_math(){
(无效)Py_InitModule(c_math,c_mathMethods);
}INT
主(INT ARGC,CHAR *的argv []){
Py_SetProgramName(的argv [0]); Py_Initialize(); initc_math();
}
和cmath.py:
从distutils.core进口设置,扩展c_math =扩展('c_math',源= ['c_math.c'])设置(名称='c_math',
版本='1.0',
说明=c_math设置,
ext_modules = [c_math])
我在做什么不正确?
PS:请,如果存在于c_math.c其他错误(我的意思不是由GCC显示错误)不说话,我想通过自己找到的错误^^
解决方案
静态PyMethodDef c_mathMethods [] = {
{更多,c_math_more,METH_VARARGS,N + NN使用C API},
{NULL,NULL,0,NULL}
- }
+};
您在末尾缺少分号...
I'm trying to build a Python Module/extension write using C API, but it fails:
% python2 cmath.py build
running build
running build_ext
building 'c_math' extension
creating build
creating build/temp.linux-x86_64-2.7
gcc -pthread -fno-strict-aliasing -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -DNDEBUG -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python2.7 -c c_math.c -o build/temp.linux-x86_64-2.7/c_math.o
c_math.c:18:5: warning: initialization from incompatible pointer type [enabled by default]
{"more", c_math_more, METH_VARARGS, "n + nn Using C API"},
^
c_math.c:18:5: warning: (near initialization for ‘c_mathMethods[0].ml_meth’) [enabled by default]
In file included from /usr/include/python2.7/Python.h:58:0,
from c_math.c:1:
/usr/include/python2.7/pyport.h:802:39: error: expected ‘,’ or ‘;’ before ‘void’
# define PyMODINIT_FUNC void
^
c_math.c:22:1: note: in expansion of macro ‘PyMODINIT_FUNC’
PyMODINIT_FUNC
^
error: command 'gcc' failed with exit status 1
I've tried to build manually without distutils but i get same error/warnings messages
c_math.c:
#include <Python.h>
static PyObject *
c_math_more(PyObject *self, PyObject *n, PyObject *nn) {
if ( PyInt_Check(n) && PyInt_Check(nn) ) {
float cn, cnn, result;
cn = PyInt_AsLong(n);
cnn = PyInt_AsLong(nn);
result = cn + cnn;
return PyInt_FromLong(result);
}
}
static PyMethodDef c_mathMethods[] = {
{"more", c_math_more, METH_VARARGS, "n + nn Using C API"},
{NULL, NULL, 0, NULL}
}
PyMODINIT_FUNC
initc_math() {
(void) Py_InitModule("c_math", c_mathMethods);
}
int
main(int argc, char *argv[]) {
Py_SetProgramName(argv[0]);
Py_Initialize();
initc_math();
}
And cmath.py:
from distutils.core import setup, Extension
c_math = Extension('c_math', sources = ['c_math.c'])
setup(name = 'c_math',
version = '1.0',
description = "c_math setup",
ext_modules = [c_math])
What i'm doing incorrect ?
ps: please, if exists other errors on c_math.c (i mean errors that isn't showed by gcc) DON'T talk me, I want to find the errors by myself ^^
解决方案
static PyMethodDef c_mathMethods[] = {
{"more", c_math_more, METH_VARARGS, "n + nn Using C API"},
{NULL, NULL, 0, NULL}
- }
+ };
You are missing a semicolon at the end...
这篇关于建立在GCC的Python(2.7)模块4.8失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文