如何建立与Python中的MinGW-W64我的C扩展?(How can I build my C

2019-06-25 17:48发布

所以,我有一些Python的C扩展我以前建的和在Win7上运行32位Python的使用。 我现在已经切换不过来64位Python和我有建立C扩展使用MinGW-W64的问题。

我所做的更改的distutils按照这个帖子 ,但我得到一些奇怪的错误提示什么是错的:

$ 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

我用Google搜索周围相当多的查找信息,但它是不容易找到一个明确的答案。 可能有人阐明这一些轻? 我应该做哪些进一步的变化能够成功地打造C扩展为64位的Python在Win7?

编辑:

在cgohlke的评论了一些有用的指针后下面我设法生成libpython27.a 。 在下面的意见后,但是这个帖子 (第二到最后一个)我仍然有一个的__imp_Py_InitModule4_64错误。 一些严重的谷歌福后,我设法绊倒这篇文章告诉我重命名Py_InitModule4线Py_InitModule4_64 。 之后,一切都顺顺当当的工作。

Answer 1:

这为我工作与Python 3.3:

  1. 创建DLL静态蟒蛇LIB

    蟒DLL通常在C:/窗/ System32下; 在MSYS壳:

     gendef.exe python33.dll dlltool.exe --dllname python33.dll --def python33.def --output-lib libpython33.a mv libpython33.a C:/Python33/libs 
  2. 使用痛饮生成的包装

    例如, swig -c++ -python myExtension.i

  3. 包装必须用MS_WIN64进行编译,或者当您导入在Python类电脑会崩溃

     g++ -c myExtension.cpp -I/other/includes g++ -DMS_WIN64 -c myExtension_wrap.cxx -IC:/Python33/include 
  4. 共享库

     g++ -shared -o _myExtension.pyd myExtension.o myExtension_wrap.o -lPython33 -lOtherSharedLibs -LC:/Python33/libs -LC:/path/to/other/shared/libs 
  5. 确保所有共享库(GDAL,OtherSharedLibs)在路径(Windows不使用LD_LIBRARY_PATH或PYTHONPATH)

  6. 在Python,只是:进口myExtension

瞧!



Answer 2:

下面是一个VC的示例代码++生成工具https://github.com/starnight/python-c-extension/tree/master/00-HelloWorld

你可以试试:

python setup.py -c mingw32

然而,这不是为我工作。

我的解决办法是:

  1. 安装64位蟒蛇蟒蛇3.6

  2. 安装mingw64

  3. 添加mingw64 / bin添加到PATH
  4. 从编译C文件的dll通过

     gcc -c libmypy.c -IC:\Users\{user_name}\Anaconda3\pkgs\python-3.6.4-h6538335_1\include gcc -shared -o libmypy.dll libmypy.o -LC:\Users\{user_name}\Anaconda3\pkgs\python-3.6.4-h6538335_1\libs -lPython36 
  5. 在脚本的.py加载DLL文件

     from ctypes import * m = cdll.LoadLibrary(r"C:\{path_to_dll}\libmypy.dll") print(m.hello()) 


文章来源: How can I build my C extensions with MinGW-w64 in Python?