为python构建C扩展——'undefined reference to _Py_GC_generation0'

build C extension for python——'undefined reference to _Py_GC_generation0'

背景资料是想用pyhton实现自己的HashMap对象进行C扩展

但是当我完成'hashmap.c'并使用'distutils'将其编译为模块时,我发现'undefined reference to _Py_GC_generation0'有问题。

'_Py_GC_generation0'引用在'objimpl.h',代码如下:

typedef union _gc_head {
    struct {
        union _gc_head *gc_next;
        union _gc_head *gc_prev;
        Py_ssize_t gc_refs;
    } gc;
    double dummy;  /* force worst-case alignment */
} PyGC_Head;

extern PyGC_Head *_PyGC_generation0;

'_PyGC_generation0'实际定义在'gcmodule.c',代码如下:

static struct gc_generation generations[NUM_GENERATIONS] = {
    /* PyGC_Head,                               threshold,      count */
    {{{GEN_HEAD(0), GEN_HEAD(0), 0}},           700,            0},
    {{{GEN_HEAD(1), GEN_HEAD(1), 0}},           10,             0},
    {{{GEN_HEAD(2), GEN_HEAD(2), 0}},           10,             0},
};

PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);

我尝试用'python3.lib'和'python36.lib'构建但是没有成功,还是报错'undefined reference to _Py_GC_generation0'

这是我构建模块的代码(路径正确):

from distutils.core import setup, Extension

hashmap = Extension('hashmap',
                    libraries=['python3', 'python36'],
                    library_dirs = ['E:\python3.6.6'],
                    sources = ['hashmap.c']
                    )
setup(
    name = 'C extension module',
    version = '1.0',
    description = 'hashmap',
    ext_modules = [hashmap])

谢谢大家!

正如@DavidW 所说,我不应该指定 Python 库。我在 python 文档上得到了答案。我应该只把'_Py_GC_Track()'改成'Py_GC_Track()',原因如图:enter image description here