Search code examples
python-3.xpython-c-api

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


The background information is that I want to implement my own HashMap object by pyhton for C extension.

But When I finished 'hashmap.c' and used 'distutils' to compile it as a module, I found something wrong with 'undefined reference to _Py_GC_generation0'.

'_Py_GC_generation0' is quoted at 'objimpl.h', code show as below:

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' is actually defined at 'gcmodule.c', code show as below:

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

I tried to build with 'python3.lib' and 'python36.lib' but it didn't work, It still reported the error 'undefined reference to _Py_GC_generation0'

Here is my code for build the module(the path is correct):

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

Thank you all!


Solution

  • as @DavidW said, I should not specify the Python libraries. I got the answer on python doc. I should only change '_Py_GC_Track()' to 'Py_GC_Track()', the reason as the picture:enter image description here