【问题标题】:Cython wrapper compile .c files to .pyd using cl.exe (windows)Cython 包装器使用 cl.exe (windows) 将 .c 文件编译为 .pyd
【发布时间】:2015-07-05 10:35:36
【问题描述】:

目录结构

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.libCextendedPy.objCextendedPy.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 文件中做错了,但不知道其中有什么问题。如果有人可以建议一个等效的 setup.py 代码(适用于 linux 和 windows),那就太好了。

【问题讨论】:

    标签: python c windows batch-file cython


    【解决方案1】:

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

    /c 表示“只编译,不链接”,这就是为什么你只获取目标文件。

    【讨论】:

    • 谢谢! @Nikita Nemkin 但这不仅是我必须改变的地方,而且还没有完全发挥作用
    【解决方案2】:

    我对 compile.bat 进行了以下更改,它起作用的原因是 .pyd.dll 文件没有一起创建。对于.pyd 文件,我必须使用/Fe<module name>.pyd 运行命令,对于.dll,我必须使用/OUT:<module name>.dll 运行命令

    编译.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中的问题。

    【讨论】:

      猜你喜欢
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2016-05-31
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      相关资源
      最近更新 更多