2012-06-25 41 views
24

所以我有一些Python C扩展,我以前已经为Win7中运行的32位Python使用。然而,我现在已经转向使用64位Python,而我在用MinGW-w64构建C扩展时遇到了问题。如何在Python中使用MinGW-w64构建我的C扩展?

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

$ 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搜索周围相当多的查找信息,但它不容易找到一个确定的答案。有人可以对此有所了解吗?我应该做些什么才能在Win7中成功构建64位Python的C扩展?

编辑:

在cgohlke的评论一些有用的指针下面我设法生成libpython27.a后。但是,在遵循this post(倒数第二)的建议后,我仍然有__imp_Py_InitModule4_64错误。在一些严重的Google-fu之后,我设法跳过this post告诉我将Py_InitModule4行重命名为Py_InitModule4_64。之后,一切都顺利进行。

+11

您需要使用'gendef.exe python27.dll'和'dlltool.exe --dllname python27.dll --def python27.def --output-lib libpython27.a'创建'libpython27.a'并放置它在'C:\ Python27 \ libs' – cgohlke

+0

我看到一些提到gendef的帖子。但找到关于如何生成libpython27.a文件的信息并不容易。你会碰巧知道吗? – c00kiemonster

+0

呵呵,gendef和dlltool是两个不同的命令。会尝试。谢谢。 – c00kiemonster

回答

11

这为我工作与Python 3.3:

  1. 创建DLL静态蟒蛇的lib

    蟒蛇DLL通常是C:/ Windows的/ 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)是在你的PATH (Windows不使用LD_LIBRARY_PATH或PYTHONPATH)

  6. 在Python

    ,只是:进口myExtension

瞧!

相关问题