已尝试按照此示例“Python extensions with C libraries made easy by Cython”进行操作,但我无法正常工作。我的系统适用于普通的 Cython。在 setup.py 中做了一些小改动(因为我使用的是 Windows,所以必须使用 setuptools 而不是 distutils)。制作了以下文件:
* cmean.h */
double mean(int, double*);
/* cmean.c */
double mean(int n, double* a)
{
double s;
int i;
for (s=0., i=0; i<n; i++) s+=*(a++);
return s/n;
}
# m.pyx
cdef extern from "cmean.h":
double mean(int, double*)
from stdlib cimport *
def cmean(a):
n = len(a)
cdef double *v
v = malloc(n*sizeof(double))
for i in range(n):
v[i] = float(a[i])
m = mean(n, v)
free(v)
return m
#setup.py
from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
ext_modules=[Extension("lib",
["m.pyx"],
library_dirs = ['.'],
libraries=["cmean"]
)] # Unix-like specific
setup(
name = "Demos",
ext_modules = cythonize(ext_modules),
cmdclass = {"build_ext": build_ext}
)
使用以下命令编译 cmean.c:
gcc -shared cmean.c -o libcmean.so
但是当我运行时:
python setup.py build_ext --inplace
我收到这些错误消息:
E:\GD\UD\Software\CythonTest\calling-c\test1>python setup.py build_ext --inplace
running build_ext
building 'lib' extension
C:\Users\trofl\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -I. -IC:\Anaconda2\include -IC:\Anaconda2\PC /Tcm.c /Fobuild\temp.win32-2.7\Release\m.objm.c
C:\Users\trofl\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:. /LIBPATH:C:\ /LIBPATH:C:\Anaconda2\libs /LIBPATH:C:\Anaconda2\PCbuild /LIBPATH:C:\Anaconda2\PC\VS9.0 cmean.lib /EXPORT:initlib build\temp.win32-2.7\Release\m.obj /OUT:E:\GD\UD\Software\CythonTest\calling-c\test1\lib.pyd /IMPLIB:build\temp.win32-2.7\Release\lib.lib /MANIFESTFILE:build\temp.win32-2.7\Release\lib.pyd.manifest
LINK : fatal error LNK1181: cannot open input file 'cmean.lib'
error: command 'C:\\Users\\trofl\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\link.exe' failed with exit status 1181
我已经尽我所能地遵循了这个例子。
更新 我制作了一个 cmean.lib 文件,因为最后一条错误消息说它没有在 Microsoft Visual Studio 2008 x86 工具的帮助下找到。尝试使用与 cython 使用的相同的标志。我已经尝试阅读很多有关此标志的含义的文章,但我发现其中很多内容都非常技术性:
cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG cmean.c
然后制作 lib 文件:
lib.exe lib.exe/out:cmean cmean.obj
但是不,我得到了这个错误:
E:\GD\UD\Software\CythonTest\calling-c\test1>python setup.py build_ext --inplace
running build_ext
building 'lib' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
C:\Users\trofl\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Anaconda2\include -IC:\Anaconda2\PC /Tcm.c /Fobuild\temp.win32-2.7\Release\m.objm.c
C:\Users\trofl\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:. /LIBPATH:C:\Anaconda2\libs /LIBPATH:C:\Anaconda2\PCbuild /LIBPATH:C:\Anaconda2\PC\VS9.0 cmean.lib /EXPORT:initlib build\temp.win32-2.7\Release\m.obj /OUT:E:\GD\UD\Software\CythonTest\calling-c\test1\lib.pyd /IMPLIB:build\temp.win32-2.7\Release\lib.lib /MANIFESTFILE:build\temp.win32-2.7\Release\lib.pyd.manifest
LINK : error LNK2001: unresolved external symbol initlib
build\temp.win32-2.7\Release\lib.lib : fatal error LNK1120: 1 unresolved externals
error: command 'C:\\Users\\trofl\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\link.exe' failed with exit status 1120
我假设我必须找出用于 visual c++ for windows 的参数,以便以正确的格式制作 cmean.lib 文件。
最佳答案
我终于设法通过一个非常简单的修复让它工作。必须使用与 pyx 文件相同的库名称:
ext_modules=[Extension("m",
sources = ["m.pyx"],
library_dirs = ['.'],
libraries=["cmean"])] # Unix-like specific
这在这篇文章中也有说明:
Cython compiled C extension: ImportError: dynamic module does not define init function
我还发现我可以用 setuptools 编译 cmean.c 文件:
#setup.py
from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
ext_modules=[Extension("m",
["m.pyx","cmean.c"] )]
setup(
name = "Demos",
ext_modules = cythonize(ext_modules),
cmdclass = {"build_ext": build_ext}
)
– 福塞卡尔
关于Cython:LINK: fatal error LNK1181:无法打开输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34425131/