在Windows7 Py_InitModule4上使用mingw_x64编译的python_x64 + C库

时间:2013-09-10 11:39:36

标签: python c gcc windows-7-x64 mingw-w64

我正在尝试使用mingw-x64在Windows7(64位)上为python编译C库。 这一切都像32位版本的魅力。

我曾经用我的库编译 gcc -shared -IC:\ Python27 \ include -LC:\ Python27 \ libs myModule.c -lpython27 -o myModule.pyd

它适用于32位版本。相同的过程适用于64位Linux。 但是在64位windows7上(使用64位x86_64-w64-mingw32和64位python 2.7.5)我遇到了一个问题:

C:\Users\sergej\AppData\Local\Temp\cci8TbXw.o:myModule.c:(.text+0x267): 
undefined reference to `__imp_Py_InitModule4'
collect2: ld returned 1 exit status

我检查了C:/Python27/libs/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

我目前没有想法该怎么做。有什么建议吗? 这里的C代码不是问题。我的例子来自同样的问题 http://csl.name/C-functions-from-Python/
注 - 本例中第26行的拼写错误:应为VARARGS

是的 - 我确实发现(类似于How can I build my C extensions with MinGW-w64 in Python?问题)我可以通过将-DMS_WIN64添加到gcc行来编译这个简单的例子,但我的真实程序中仍然有类似的错误(表明还有更多它)

undefined reference to `__imp_PyArg_ParseTuple'
undefined reference to `__imp_Py_BuildValue'
undefined reference to `__imp_Py_InitModule4_64'

1 个答案:

答案 0 :(得分:9)

从评论中复制答案,以便从"未答复的"中删除此问题。过滤器:

  

我讨厌回答我自己的问题,但是......添加-DMS_WIN64实际上已经足够了。剩下的问题是由于gcc参数(由于某种原因-lpython27应该在-o myModule.pyd之前发生),这在我的项目中没有正确的顺序

〜每Sergej Srepfler

回答