initializer is not a constant,错误 C2099,在为 python 编译用 c 编写的模块时
initializer is not a constant, error C2099, on compiling a module written in c for python
我试图在 windows 10 上使用 msvc 2017 编译一个名为 distance 的 python 模块,"python setup.py install --with-c",我得到了这个错误,
Cdistance / distance.c (647): error C2099: initializer is not a
constant
Cdistance / distance.c (689): error C2099: initializer is not a
constant
Error: command 'C: \ Program Files (x86) \ Microsoft Visual Studio \
2017 \ BuildTools \ VC \ Tools \ MSVC \ 14.10.25017 \ bin \ HostX64 \
x64 \ cl .exe 'failed with exit status 2
这是第 647 行的代码
646 PyTypeObject IFastComp_Type = {
647 PyVarObject_HEAD_INIT(&PyType_Type, 0)
648 "distance.ifast_comp", /* tp_name */
649 sizeof(ItorState), /* tp_basicsize */
650 0, /* tp_itemsize */
(destructor)itor_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
ifast_comp_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)ifastcomp_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
ifastcomp_new, /* tp_new */
};
第689行是另一个类似的结构,
688 PyTypeObject ILevenshtein_Type = {
689 PyVarObject_HEAD_INIT(&PyType_Type, 0)
"distance.ilevenshtein", /* tp_name */
sizeof(ItorState), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)itor_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
两者在同一页中引用如下
762 if (PyType_Ready(&IFastComp_Type) != 0 || PyType_Ready(&ILevenshtein_Type)!= 0)
763 #if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif
Py_INCREF((PyObject *)&IFastComp_Type);
Py_INCREF((PyObject *)&ILevenshtein_Type);
谢谢
我找到了解决方案,通过查看结构 PyTypeObject PyTypeObject 的定义,我将 yVarObject_HEAD_INIT(&PyType_Type, 0)
更改为 PyVarObject_HEAD_INIT(NULL, 0)
并成功编译,我已经尝试了一些功能和它的工作原理,所以错误是由 &PyType_Type
引起的,它是一个 PyObject*
,我知道因为 IFastComp_Type
是一个全局变量,它应该由一个 constante 初始化,但我仍然不不知道模块作者为什么给&PyType_Type
作为参数,谢谢大家的意见。
参见 the documentation for "defining new types"。
PyVarObject_HEAD_INIT(NULL, 0)
This line is a bit of a wart; what we’d like to write is:
PyVarObject_HEAD_INIT(&PyType_Type, 0)
as the type of a type object is “type”, but this isn’t strictly conforming C and some compilers complain. Fortunately, this member will be filled in for us by PyType_Ready()
.
我认为 Visual C 是有问题的编译器,并且该模块是用 GCC 编写和测试的...
我试图在 windows 10 上使用 msvc 2017 编译一个名为 distance 的 python 模块,"python setup.py install --with-c",我得到了这个错误,
Cdistance / distance.c (647): error C2099: initializer is not a constant
Cdistance / distance.c (689): error C2099: initializer is not a constant
Error: command 'C: \ Program Files (x86) \ Microsoft Visual Studio \ 2017 \ BuildTools \ VC \ Tools \ MSVC \ 14.10.25017 \ bin \ HostX64 \ x64 \ cl .exe 'failed with exit status 2
这是第 647 行的代码
646 PyTypeObject IFastComp_Type = {
647 PyVarObject_HEAD_INIT(&PyType_Type, 0)
648 "distance.ifast_comp", /* tp_name */
649 sizeof(ItorState), /* tp_basicsize */
650 0, /* tp_itemsize */
(destructor)itor_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
ifast_comp_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)ifastcomp_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
ifastcomp_new, /* tp_new */
};
第689行是另一个类似的结构,
688 PyTypeObject ILevenshtein_Type = {
689 PyVarObject_HEAD_INIT(&PyType_Type, 0)
"distance.ilevenshtein", /* tp_name */
sizeof(ItorState), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)itor_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
两者在同一页中引用如下
762 if (PyType_Ready(&IFastComp_Type) != 0 || PyType_Ready(&ILevenshtein_Type)!= 0)
763 #if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif
Py_INCREF((PyObject *)&IFastComp_Type);
Py_INCREF((PyObject *)&ILevenshtein_Type);
谢谢
我找到了解决方案,通过查看结构 PyTypeObject PyTypeObject 的定义,我将 yVarObject_HEAD_INIT(&PyType_Type, 0)
更改为 PyVarObject_HEAD_INIT(NULL, 0)
并成功编译,我已经尝试了一些功能和它的工作原理,所以错误是由 &PyType_Type
引起的,它是一个 PyObject*
,我知道因为 IFastComp_Type
是一个全局变量,它应该由一个 constante 初始化,但我仍然不不知道模块作者为什么给&PyType_Type
作为参数,谢谢大家的意见。
参见 the documentation for "defining new types"。
PyVarObject_HEAD_INIT(NULL, 0)
This line is a bit of a wart; what we’d like to write is:
PyVarObject_HEAD_INIT(&PyType_Type, 0)
as the type of a type object is “type”, but this isn’t strictly conforming C and some compilers complain. Fortunately, this member will be filled in for us by
PyType_Ready()
.
我认为 Visual C 是有问题的编译器,并且该模块是用 GCC 编写和测试的...