【问题标题】:Build Python (2.7) module on GCC 4.8 fails在 GCC 4.8 上构建 Python (2.7) 模块失败
【发布时间】:2013-04-24 00:53:54
【问题描述】:

我正在尝试使用 C API 构建 Python 模块/扩展编写,但它失败了:

% 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

我尝试在没有 distutils 的情况下手动构建,但我收到相同的错误/警告消息

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

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

我做错了什么?

ps:如果 c_math.c 上存在其他错误(我的意思是 gcc 未显示的错误),请不要跟我说,我想自己找到错误^^

【问题讨论】:

    标签: c gcc python-2.7 compilation


    【解决方案1】:
      static PyMethodDef c_mathMethods[] = {
          {"more", c_math_more, METH_VARARGS, "n + nn Using C API"},
          {NULL, NULL, 0, NULL}
    - }
    + };
    

    最后少了一个分号...

    【讨论】:

    • 谢谢,编译后...我还有一个问题,警告“警告:从不兼容的指针类型初始化[默认启用]”如何解决这个问题?还有“警告:('c_mathMethods[0].ml_meth'的接近初始化)[默认启用]”
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2016-11-14
    • 2014-08-26
    • 2015-11-29
    • 2017-09-12
    • 1970-01-01
    • 2021-04-27
    • 2019-12-26
    相关资源
    最近更新 更多