问题
I'm trying to compile C library for python on Windows7 (64-bit) using mingw-x64. It all worked like a charm with 32-bit versions.
I used to compile my library with gcc -shared -IC:\Python27\include -LC:\Python27\libs myModule.c -lpython27 -o myModule.pyd
and it worked with 32-bit versions. The same procedure is working with 64-bit linux. But on 64-bit windows7 (using 64-bit x86_64-w64-mingw32 and 64-bit python 2.7.5) I have a problem:
C:\Users\sergej\AppData\Local\Temp\cci8TbXw.o:myModule.c:(.text+0x267):
undefined reference to `__imp_Py_InitModule4'
collect2: ld returned 1 exit status
I checked C:/Python27/libs/modsupport.h and it already contains
#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
I'm currently out of ideas what to do. Any suggestion?
The C code is not an issue here. I have the same problem with example from
http://csl.name/C-functions-from-Python/
Note - typo in line 26 in this example: should be VARARGS
And yes - I did found (similar to How can I build my C extensions with MinGW-w64 in Python? question ) that I can compile this trivial example by adding -DMS_WIN64 to gcc line, but I still got the similar errors in my real program (suggesting there is more to it)
undefined reference to `__imp_PyArg_ParseTuple'
undefined reference to `__imp_Py_BuildValue'
undefined reference to `__imp_Py_InitModule4_64'
回答1:
Copying the answer from the comments in order to remove this question from the "Unanswered" filter:
I hate to answer my own questions, but... adding -DMS_WIN64 is actually enough. Remaining problems were due to gcc parameters ( for some reason -lpython27 should go right before -o myModule.pyd), which were not in correct order in my project
~ answer per Sergej Srepfler
来源:https://stackoverflow.com/questions/18717996/python-x64-c-library-compiled-with-mingw-x64-on-windows7-py-initmodule4