How can I build my C extensions with MinGW-w64 in Python?

64-bit, c, compilation, python

Solution

This worked for me with Python 3.3 :

create static python lib from dll

python dll is usually in C:/Windows/System32; in msys shell:

gendef.exe python33.dll

dlltool.exe --dllname python33.dll --def python33.def --output-lib libpython33.a

mv libpython33.a C:/Python33/libs

use swig to generate wrappers

e.g., `swig -c++ -python myExtension.i`

wrapper MUST be compiled with MS_WIN64, or your computer will crash when you import the class in Python

g++ -c myExtension.cpp -I/other/includes

g++ -DMS_WIN64 -c myExtension_wrap.cxx -IC:/Python33/include

shared library

g++ -shared -o _myExtension.pyd myExtension.o myExtension_wrap.o -lPython33 -lOtherSharedLibs -LC:/Python33/libs -LC:/path/to/other/shared/libs

make sure all shared libs (gdal, OtherSharedLibs) are in your PATH (windows does not use LD_LIBRARY_PATH or PYTHONPATH)

in Python, just: import myExtension

voila!

Problem

So I have a few Python C extensions I have previously built for and used in 32 bit Python running in Win7. I have now however switched to 64 bit Python, and I am having issues building the C extension with MinGW-w64. I made the changes to distutils as per this post, but I am getting some weird errors suggesting something is wrong: ``` $ python setup.py build running build running build_ext building 'MyLib' extension c:\MinGW64\bin\x86_64-w64-mingw32-gcc.exe -mdll -O -Wall -Ic:\Python27\lib\site-packages\numpy\core\include -Ic:\Python27\include -Ic:\Python27\PC -c MyLib.c -o build\temp.win-amd64-2.7\Release\mylib.o MyLib.c: In function 'initMyLib': MyLib.c:631:5: warning: implicit declaration of function 'Py_InitModule4_64' [-Wimplicit-function-declaration] writing build\temp.win-amd64-2.7\Release\MyLib.def c:\MinGW64\bin\x86_64-w64-mingw32-gcc.exe -shared -s build\temp.win-amd64-2.7\Release\mylib.o build\temp.win-amd64-2.7\Release\MyLib.def -Lc:\Python27\libs -Lc:\Python27\PCbuild\amd64 -lpython27 -o build\lib.win-amd64-2.7\MyLib.pyd build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x13d): undefined reference to `__imp_PyExc_ValueError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1275): undefined reference to `__imp_PyExc_ValueError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1eef): undefined reference to `__imp_PyExc_ImportError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1f38): undefined reference to `__imp_PyExc_AttributeError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1f4d): undefined reference to `__imp_PyCObject_Type' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1f61): undefined reference to `__imp_PyExc_RuntimeError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1fc7): undefined reference to `__imp_PyExc_RuntimeError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1ffe): undefined reference to `__imp_PyExc_RuntimeError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x2042): undefined reference to `__imp_PyExc_RuntimeError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x206c): undefined reference to `__imp_PyExc_RuntimeError' build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x208a): more undefined references to `__imp_PyExc_RuntimeError' follow build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x20a7): undefined reference to `__imp_PyExc_ImportError' collect2.exe: error: ld returned 1 exit status error: command 'x86_64-w64-mingw32-gcc' failed with exit status 1 ``` I have googled around quite a bit to find information, but it's not easy to find a definite answer. Could someone shed some light on this? What further changes should I do to be able to successfully build C extensions for 64 bit Python in Win7? EDIT: After some helpful pointers in cgohlke's comments below I managed to generate `libpython27.a`. However after following the advice on this post (2nd to last) I still had a the `__imp_Py_InitModule4_64` error. After some serious Google-fu I managed to trip over this post telling me to rename the `Py_InitModule4` line to `Py_InitModule4_64`. After that everything worked swimmingly.

Original source