9

我正在尝试使用 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? question )我可以通过将 -DMS_WIN64 添加到 gcc 行来编译这个简单的例子,但我仍然在我的真实中遇到类似的错误程序(建议还有更多)

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

1 回答 1

9

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

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

〜回答每个Sergej Srepfler

于 2013-10-10T05:01:45.147 回答