2

尝试使用 Cygwin 中的 gcc 编译此Python 文档文章开头的示例。我的代码:

#include <Python.h>

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return PyLong_FromLong(sts);
}

但是,在尝试编译时,gcc 给了我以下错误:

In file included from /usr/include/Python.h:77:0,
                 from test.c:1:
/usr/include/longobject.h:77:26: error: expected '=', ',', ';', 'asm' or '__attr
ibute__' before 'PyLong_AsLongLong'
/usr/include/longobject.h:78:35: error: expected '=', ',', ';', 'asm' or '__attr
ibute__' before 'PyLong_AsUnsignedLongLong'
/usr/include/longobject.h:79:35: error: expected '=', ',', ';', 'asm' or '__attr
ibute__' before 'PyLong_AsUnsignedLongLongMask'
/usr/include/longobject.h:80:26: error: expected '=', ',', ';', 'asm' or '__attr
ibute__' before 'PyLong_AsLongLongAndOverflow'

有任何想法吗?

更新:

显然这是由 PY_LONG_LONG 扩展到 __int64 引起的,除非我在 Python.h 之前包含 windows.h,否则它没有定义。但是,我不明白为什么必须这样做,并且它违反了链接文章中的以下规则:“由于 Python 可能定义了一些影响某些系统上的标准头文件的预处理器定义,因此您必须在之前包含 Python.h包括任何标准标题。”

有什么方法可以避免必须包含 windows.h?

4

0 回答 0