Cython包装使用cl.exe将.c文件编译为.pyd(windows)

目录结构

Cextended_API_504/ lib/ Cextended.dll Cextended.lib libcextended.a src/ Cextended.h CextendedEx.c CextendedEx.h example/ Demo.c compileDemo.bat compileDemo.sh CextendedPy.pyx compile.bat compile.sh 

CextendedPy.pyx

 cdef extern from "Cextended.h": ... cdef extern from "CextendedEx.h": ... cdef class wrapper: ... 

Cextended_API_504 / example / compileDemo.sh [linux] (正常工作)

 #!/bin/sh gcc ../src/CextendedEx.c -c -fPIC -I. -L../bin cp -f CextendedEx.o ../lib/CextendedEx.o gcc ../lib/CextendedEx.o Demo.c -o Demo -I../src -L../lib -lrt -lcextended -lpthread -lstdc++ -lm 

Cextended_API_504 / example / compileDemo.bat [windows] (正常工作)

 echo off CLS set msvc=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\ call "%msvc%vcvarsall.bat" x86_amd64 "%msvc%VC\bin\%PROCESSOR_ARCHITECTURE%\cl.exe" /c /ICextended_API_504\src\ /ICextended_API_504\src\CextendedEx.c /linkCextended_API_504\lib\Cextended.lib COPY *.obj "Cextended_API_504\lib" copy ..\lib\Cextended.dll rem echo "----------------------- COMPILE STEP Demo.c" rem "%msvc%bin\x86_amd64\cl.exe" /c /I..\src Demo.c rem echo "----------------------- LINK STEP Demo.c" rem LINK /LIBPATH:..\lib Cextended.lib CextendedEx.obj Demo.obj /OUT:Demo.exe echo "----------------------------------- COMPILE & LINK" echo "----------------------------------- Demo.c "%msvc%bin\x86_amd64\cl.exe" /I..\src Demo.c ..\src\CextendedEx.c /link ..\lib\Cextended.lib /OUT:Demo.exe 

compile.sh (成功编译成CextendedPy.so) [linux]

 #!/bin/sh echo "compiling" gcc Cextended_API_504/src/CextendedEx.c -c -fPIC -I. -LCextended_API_504/bin cp -f CextendedEx.o Cextended_API_504/lib/CextendedEx.o #compiling cython to c cython -a CextendedPy.pyx #compiling C to .so file gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 Cextended_API_504/lib/CextendedEx.o CextendedPy.c -o CextendedPy.so -ICextended_API_504/src -LCextended_API_504/lib -lrt -lcextended -lpthread -lstdc++ -lm 

compile.bat (以下代码错误) [窗口]

 set msvc=%userprofile%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\ call "%msvc%vcvarsall.bat" %PROCESSOR_ARCHITECTURE% copy"Cextended_API_504\lib\Cextended.dll" "%msvc%VC\bin\%PROCESSOR_ARCHITECTURE%\cl.exe" /c /ICextended_API_504\src\ /ICextended_API_504\src\CextendedEx.c /linkCextended_API_504\lib\Cextended.lib COPY *.obj "Cextended_API_504\lib" cython -a CextendedPy.pyx "%msvc%VC\bin\%PROCESSOR_ARCHITECTURE%\cl.exe" /c /nologo /Ox /MD /W3 /GS- /DNDEBUG /I. /ICextended_API_504\src\ /IC:\Python27\include /IC:\Python27\PC /FeCextendedPy.pyd CextendedPy.c Cextended_API_504\src\CextendedEx.c /linkCextended_API_504\lib\Cextended.lib /dll /libpath:C:\Python27\libs 

不通过任何错误,创buildCextendedPy.libCextendedPy.obj,但不创buildCextendedPy.pydCextendedPy.dll

我尝试setup.py(在Windows中,但没有工作) (它无法链接函数在C头文件)

 from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ Extension("CextendedPy", ["CextendedPy.pyx"], include_dirs = ["Cextended_API_504\\src\\"], libraries = ["Cextended_API_504\\lib\\Cextended.lib"], library_dirs = ["Cextended_API_504\\lib\\"]) ] setup( name = "Probability Cextended", ext_modules = cythonize(extensions), ) 

在Linux的代码工作正常,只在Windows不工作。 我知道我在compile.bat文件中做了一些错误的事情,但不知道什么是错的。 如果任何人都可以build议一个等效的setup.py代码(这将适用于Linux和Windows)将是伟大的。

从第二次调用cl.exe删除/c标志。

/c意思是“只编译,不要链接”,这就是为什么你只能得到对象文件。

我做了.pyd修改compile.bat和它背后的原因是.pyd.dll文件都没有创建在一起。 我必须使用/Fe<module name>.pyd运行命令为.pyd文件和/OUT:<module name>.dll.dll

的compile.bat

 set msvc=%userprofile%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\ call "%msvc%vcvarsall.bat" %PROCESSOR_ARCHITECTURE% copy"Cextended_API_504\lib\Cextended.dll" "%msvc%VC\bin\%PROCESSOR_ARCHITECTURE%\cl.exe" /c /ICextended_API_504\src\ /ICextended_API_504\src\CextendedEx.c /linkCextended_API_504\lib\Cextended.lib COPY *.obj "Cextended_API_504\lib" cython -a CextendedPy.pyx "%msvc%VC\bin\%PROCESSOR_ARCHITECTURE%\cl.exe" /nologo /LD /W4 /ICextended_API_504\src\ /IC:\Python27\include /IC:\Python27\PC /IC:\Python27\include /IC:\Python27\PC /FeCextendedPy.pyd CextendedPy.c /linkCextended_API_504\lib\Cextended.lib /dll /libpath:C:\Python27\libs "%msvc%VC\bin\%PROCESSOR_ARCHITECTURE%\cl.exe" /LD /W4 /ICextended_API_504\src\ /IC:\Python27\include /IC:\Python27\PC /IC:\Python27\include /IC:\Python27\PC CextendedPy.c /linkCextended_API_504\lib\Cextended.lib /dll /libpath:C:\Python27\libs /OUT:CextendedPy.dll 

在做了上面的修改之后,左右函数一直抛出错误

error LNK2019: unresolved external symbol __imp_PrintToMessagesWindow_ns referenced in function __pyx_pf_8CextendedPy_6wrapper_50PrintToMessagesWindows_ns ,当我评论所有容易出错的函数,它工作正常。

让我吃惊的是类似的功能,易于错误的功能工作,所以我检查了头文件的声明,我发现头文件中的声明。 在linux中唯一的问题在windows中仍然没有问题。