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

时间:2015-07-05 10:35:37

标签: python c windows batch-file cython

目录结构

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 (以下代码中的错误) [windows]

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

没有任何错误,创建了 CextendedPy.lib CextendedPy.obj ,但是 CextendedPy.pyd CextendedPy .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 文件中做错了但不知道它有什么问题。如果有人可以建议一个等效的setup.py代码(这将适用于Linux和Windows),这将是伟大的。

2 个答案:

答案 0 :(得分:1)

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

/c表示“仅编译,不链接”,这就是您只获取目标文件的原因。

答案 1 :(得分:0)

我对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中只有问题的问题仍然没有问题。