On Tuesday, 26 July 2011 23:53:36 UTC, llw...@gmail.com wrote: > 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 " ); > Py_INCREF(Py_None); > return Py_None; > } > > static PyMethodDef example_methods[] = { > {"foo" , ex_foo, METH_VARARGS, "foo() doc string" }, > {NULL , NULL } > }; > > static struct PyModuleDef examplemodule = { > PyModuleDef_HEAD_INIT, > "example" , > "example module doc string" , > -1 , > example_methods, > NULL , > NULL , > NULL , > NULL > }; > > PyMODINIT_FUNC > PyInit_example(void ) > { > return PyModule_Create(&examplemodule); > } > > /// my setup.py > 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 setup.py build --compiler=mingw32 > > > $ python setup.py 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 > mp.win-amd64-3.1\Release\example.o > writing build\temp.win-amd64-3.1\Release\example.def > D:\Programs\MinGW\bin\gcc.exe -mno-cygwin -shared -s > build\temp.win-amd64-3.1\Re > lease\example.o build\temp.win-amd64-3.1\Release\example.def "-Lc:\Program > Files > (x86)\Python\libs" "-Lc:\Program Files (x86)\Python\PCbuild\amd64" > -lpython31 - > lmsvcr90 -o build\lib.win-amd64-3.1\example.pyd > build\temp.win-amd64-3.1\Release\example.o:example.c:(.text+0x13): undefined > ref > erence to `_imp___Py_NoneStruct' > build\temp.win-amd64-3.1\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 --------------------
You are linking against the static version of the Python library. If you link against the DLL version, this problem goes away. -Lc:\Program Files(x86)\Python\libs" should just be "-Lc:\Program Files(x86)\Python\", assuming that's where the Python DLLs are. See: https://stackoverflow.com/questions/5159353/how-can-i-get-rid-of-the-imp-prefix-in-the-linker-in-vc#5159395 -- https://mail.python.org/mailman/listinfo/python-list