2012-06-25 13 views
24

Así que tengo unas pocas extensiones de Python C que he creado previamente y que utilizo en Python de 32 bits en Win7. Sin embargo, ahora he cambiado a Python de 64 bits, y tengo problemas para construir la extensión C con MinGW-w64.¿Cómo puedo construir mis extensiones C con MinGW-w64 en Python?

que realizó los cambios en distutils como por this post, pero yo estoy haciendo algunos errores extraños que sugiere que algo está mal:

$ 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 

He buscado en Google todo un poco para encontrar información, pero no es fácil de encontrar una respuesta definitiva. ¿Alguien podría arrojar algo de luz sobre esto? ¿Qué más cambios debería hacer para poder crear correctamente extensiones C para Python de 64 bits en Win7?

EDIT:

Después de algunos consejos útiles en los comentarios de abajo cgohlke que logró generar libpython27.a. Sin embargo, después de seguir el consejo en this post (penúltimo a último), todavía tenía el error __imp_Py_InitModule4_64. Después de un serio Google-fu logré tropezar con this post diciéndome que cambie el nombre de la línea Py_InitModule4 al Py_InitModule4_64. Después de eso, todo funcionó a la perfección.

+11

Necesita crear 'libpython27.a' con' gendef.exe python27.dll' y 'dlltool.exe --dllname python27.dll --def python27.def --output-lib libpython27.a' y colocarlo en 'C: \ Python27 \ libs' – cgohlke

+0

Vi algunas publicaciones mencionando gendef. Pero no es fácil encontrar información sobre cómo generar el archivo libpython27.a. ¿Sabrías saber de alguna? – c00kiemonster

+0

Oh ok, gendef y dlltool son dos comandos diferentes. Intentará. Gracias. – c00kiemonster

Respuesta

11

Esto funcionó para mí con Python 3.3:

  1. crear lib pitón estática de DLL

    pitón DLL es por lo general en C:/Windows/System32; en MSYS Shell: trago

    gendef.exe python33.dll 
    
    dlltool.exe --dllname python33.dll --def python33.def --output-lib libpython33.a 
    
    mv libpython33.a C:/Python33/libs 
    
  2. uso para generar contenedores

    por ejemplo, swig -c++ -python myExtension.i

  3. envoltorio se debe compilar con MS_WIN64, o su ordenador se bloqueará al importar la clase en Python

    g++ -c myExtension.cpp -I/other/includes 
    
    g++ -DMS_WIN64 -c myExtension_wrap.cxx -IC:/Python33/include 
    
  4. biblioteca compartida

    g++ -shared -o _myExtension.pyd myExtension.o myExtension_wrap.o -lPython33 -lOtherSharedLibs -LC:/Python33/libs -LC:/path/to/other/shared/libs 
    
  5. asegúrese de que todas las bibliotecas compartidas (gdal, OtherSharedLibs) son en su camino (Windows no utiliza LD_LIBRARY_PATH o PYTHONPATH)

  6. en Python, simplemente: myextension importación

voilá !

Cuestiones relacionadas