初始化不是一个常量,错误C2099,编译用python编写的模块

问题描述:

我试图编译一个名为distance的Python模块,带有c“python setup.py install --with-c”在windows上使用msvc 2017 10,我得到这个错误,初始化不是一个常量,错误C2099,编译用python编写的模块

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

这里是在线路689上线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 */ 
    }; 

代码被另一个像strucure,

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

感谢

+1

这条线“*行* 647”是很多行。哪个是647,哪个是689? – alk

+0

你知道如何初始化全局变量吗?其中一个符号不是常量。发现哪一个和你已经完成了。如果你不怎么样 - 购买一本好的C书,因为我们不是一个免费的学习服务 –

+0

ifast_comp_doc'声明如何和在哪里? (我只是将它与我​​们用C++编写的Python类进行了比较,我看不出有什么明显的错误)。 – Scheff

我已经找到了解决方案,通过寻找结构的定义PyTypeObject PyTypeObject引用如下,在同一页上,我有PyVarObject_HEAD_INIT(NULL, 0)改变yVarObject_HEAD_INIT(&PyType_Type, 0)并成功编译,我已经尝试了一些功能,它的工作原理,所以错误是由&PyType_Type这是一个PyObject*,我知道,因为IFastComp_Type是一个globale varailble它应该由一个常量初始化,但我仍然不知道为什么作者的模块给了&PyType_Type作为论点,谢谢大家的意见。

+0

'&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测试...

+0

感谢您的澄清 –

+0

我不确定一致性,我认为VC在这里表现不正确 –

+0

http://port70.net/~nsz/c/c11/n1570.html#6.6p9 –