Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python extensions for Win64 via GCC

Has anyone had any luck with compiling 64-bit Python extension modules for Windows using mingw64?

I have successfully compiled the extension in question with VS2008 for this platform. I've also compiled it with mingw32 (with a 32-bit python). I would prefer both builds to use GCC.

I've installed the mingw64-x86_64-w64 GCC 4.5.1 set of tools using Cygwin and convinced Python to use them. However, linking to python itself failed.

So I picked up pexports 0.44, used it to dump out a python26.def file and create libpython26.a.

Now, as in this question, the only link error I'm getting from Python is about __imp_py_InitModule4. Browsing through the def file, I see a Py_InitModule4_64 symbol.

Any ideas?

like image 221
kwatford Avatar asked Sep 23 '10 12:09

kwatford


2 Answers

I find you need to define MS_WIN64 as well as WIN32, also the distutils package does not understand mingw64, see this post, and this one

Patching distutils to support mingw64 is fairly trivial.

like image 117
Jason Morgan Avatar answered Oct 13 '22 20:10

Jason Morgan


There is a mechanism in Python to prevent linking a module against the wrong version of the library. The Py_InitModule4 function is renamed to Py_InitModule4_64 (via a macro) when the library / module is compiled for a 64-bit architecture (see modsupport.h) :

#if SIZEOF_SIZE_T != SIZEOF_INT
/* On a 64-bit system, rename the Py_InitModule4 so that 2.4
   modules cannot get loaded into a 2.5 interpreter */
#define Py_InitModule4 Py_InitModule4_64
#endif

So, if you're getting this error, this means either your Python library, or your Python module is compiled for a 32-bit architecture while the other one is compiled for a 64-bit architecture.

like image 30
Sylvain Defresne Avatar answered Oct 13 '22 20:10

Sylvain Defresne