【发布时间】: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.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),那就太好了。
【问题讨论】:
标签: python c windows batch-file cython