problem in compiling C API in mingw

llwaeva at llwaeva at
Wed Jul 27 01:53:36 CEST 2011

Hi all again, 
  I wonder if so far only Python 2.5.x support c extension. I try the
MSVC 2010 and 2008, also try mingw (gcc 4.x.x) and swig. But even I try
the simplest example, it said 

example_wrap.o:example_wrap.c:(.text+0x10ac): undefined reference to `_imp__PyObject_Free'
example_wrap.o:example_wrap.c:(.text+0x10f5): undefined reference to `_imp__PyCapsule_Import'
example_wrap.o:example_wrap.c:(.text+0x1100): undefined reference to `_imp__PyErr_Occurred'
example_wrap.o:example_wrap.c:(.text+0x110e): undefined reference to `_imp__PyErr_Clear'
example_wrap.o:example_wrap.c:(.text+0x1125): undefined reference to `_imp__PyImport_AddModule'
example_wrap.o:example_wrap.c:(.text+0x1144): undefined reference to `_imp__PyCapsule_New'
example_wrap.o:example_wrap.c:(.text+0x1165): undefined reference to `_imp__PyModule_AddObject'
example_wrap.o:example_wrap.c:(.text+0x1170): undefined reference to `_imp__PyBaseObject_Type'
example_wrap.o:example_wrap.c:(.text+0x11b2): undefined reference to `_imp__PyObject_SetAttr'
example_wrap.o:example_wrap.c:(.rdata+0x1e8): undefined reference to `PyObject_GenericGetAttr'
collect2: ld returned 1 exit status

and some sort like that.

So I try to use the compiler itself and get rid of all 3rd part tool,
here is what I did

///////////// source code
#include "Python.h"

static  PyObject *
ex_foo(PyObject *self, PyObject *args)
    printf("Hello, world n " );
    return  Py_None;

static  PyMethodDef example_methods[] = {
    {"foo" , ex_foo, METH_VARARGS, "foo() doc string" },
    {NULL , NULL }

static  struct  PyModuleDef examplemodule = {
    "example" ,
    "example module doc string" ,
    -1 ,
    NULL ,
    NULL ,
    NULL ,

PyInit_example(void )
    return  PyModule_Create(&examplemodule);

/// my
from distutils.core import setup, Extension

module1 = Extension('example', sources = ['example.c'])

setup (name = 'example', version = '1.0', description = 'This is a demo package', ext_modules = [module1])

// running
python build --compiler=mingw32

$ python build --compiler=mingw32
running build
running build_ext
building 'example' extension
D:\Programs\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-Ic:\Program Files (x8
6)\Python\include" "-Ic:\Program Files (x86)\Python\PC" -c example.c -o build\te\Release\example.o
writing build\\Release\example.def
D:\Programs\MinGW\bin\gcc.exe -mno-cygwin -shared -s build\\Re
lease\example.o build\\Release\example.def "-Lc:\Program Files
 (x86)\Python\libs" "-Lc:\Program Files (x86)\Python\PCbuild\amd64" -lpython31 -
lmsvcr90 -o build\\example.pyd
build\\Release\example.o:example.c:(.text+0x13): undefined ref
erence to `_imp___Py_NoneStruct'
build\\Release\example.o:example.c:(.text+0x32): undefined ref
erence to `_imp__PyModule_Create2'
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
--------------------- Original Message Ends --------------------

More information about the Python-list mailing list