c-Cython:LINK:致命错误LNK1181:无法打开输入文件



我曾尝试过以"Cython让Python扩展和C库变得容易"为例,但我没有成功。我的系统可以和普通的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:GDUDSoftwareCythonTestcalling-ctest1>python setup.py build_ext --inplace
running build_ext
building 'lib' extension
C:UserstroflAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0VCBincl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -I. -IC:Anaconda2include -IC:Anaconda2PC /Tcm.c /Fobuildtemp.win32-2.7Releasem.objm.c
C:UserstroflAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0VCBinlink.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:. /LIBPATH:C: /LIBPATH:C:Anaconda2libs /LIBPATH:C:Anaconda2PCbuild /LIBPATH:C:Anaconda2PCVS9.0 cmean.lib /EXPORT:initlib buildtemp.win32-2.7Releasem.obj /OUT:E:GDUDSoftwareCythonTestcalling-ctest1lib.pyd /IMPLIB:buildtemp.win32-2.7Releaselib.lib /MANIFESTFILE:buildtemp.win32-2.7Releaselib.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:GDUDSoftwareCythonTestcalling-ctest1>python setup.py build_ext --inplace
running build_ext
building 'lib' extension
creating build
creating buildtemp.win32-2.7
creating buildtemp.win32-2.7Release
C:UserstroflAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0VCBincl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:Anaconda2include -IC:Anaconda2PC /Tcm.c /Fobuildtemp.win32-2.7Releasem.objm.c
C:UserstroflAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0VCBinlink.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:. /LIBPATH:C:Anaconda2libs /LIBPATH:C:Anaconda2PCbuild /LIBPATH:C:Anaconda2PCVS9.0 cmean.lib /EXPORT:initlib buildtemp.win32-2.7Releasem.obj /OUT:E:GDUDSoftwareCythonTestcalling-ctest1lib.pyd /IMPLIB:buildtemp.win32-2.7Releaselib.lib /MANIFESTFILE:buildtemp.win32-2.7Releaselib.pyd.manifest
LINK : error LNK2001: unresolved external symbol initlib
buildtemp.win32-2.7Releaselib.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

我认为,为了使cmean.lib文件的格式正确,我必须找出在windows的visual c++中使用哪些参数。

我终于通过一个非常简单的修复程序使它工作起来。必须使用与pyx文件相同的库名称:

ext_modules=[Extension("m",
   sources = ["m.pyx"],
   library_dirs = ['.'],
   libraries=["cmean"])] # Unix-like specific

这也在这篇文章中指出:

Cython编译的C扩展:ImportError:动态模块没有定义init函数

我还发现我可以使用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}
    )

–fossekall

相关内容

最新更新