编译python c模块_编译Python 3.x的Python C模块时,未定义PY_MAJOR_VERSION

我正在尝试升级一个小的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传递给编译器?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值