我正在尝试升级一个小的C模块以使用Python 3.x并且无法编译它。我现在的障碍是预处理器定义了我应该用来检查Python版本无效。
该模块目前包含两个.c文件(我暂时将其余部分注释掉了)。在这两个文件中,PY_MAJOR_VERSION都是未定义的,因此编译器无法在需要的地方使用Python 3.x特定的定义。
mymodule.c的:
#ifndef PY_MAJOR_VERSION
#error Major version not defined!
#endif
#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif
#include "Python.h"
#include "myobj.h"
/* omitted: irrelevant boilerplate structs */
PyMODINIT_FUNC
initmymodule(void)
{
PyObject* m;
#ifdef PY3K
m = PyModule_Create(&mymodule_struct);
#else
(void) Py_InitModule("mymodule", MyModMethods);
m = Py_InitModule3("mymodule", NULL,
"My Module");
#endif
/* omitted: the rest of the module init code */
}myobj.c:
#ifndef PY_MAJOR_VERSION
#error Major version not defined!
#endif
#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif
#include "Python.h"
#define NEED_STATIC
#include "myobj.h"
#undef NEED_STATIC
#ifdef PY3K
#define PYSTR_FROMC PyUnicode_FromString
#define PYSTR_FORMAT PyUnicode_Format
#define PYINT_FROMC PyLong_FromLong
#else
#define PYSTR_FROMC PyString_FromString
#define PYSTR_FORMAT PyString_Format
#define PYINT_FROMC PyInt_FromLong
#endif
/* omitted: rest of module code */setup.py:
from distutils.core import setup, Extension
module1 = Extension('mymodule', sources = ['mymodule.c', 'myobj.c'])
setup(name='mymodule', version='0.1', ext_modules=[module1])我正在使用c:\python31\python setup.py bdist_wininst构建
PY_MAJOR_VERSION应该在哪里定义?是否需要告诉distutils传递给编译器?