【问题标题】:Cython: LINK : fatal error LNK1181: cannot open input fileCython:链接:致命错误 LNK1181:无法打开输入文件
【发布时间】:2018-01-17 21:43:58
【问题描述】:

已尝试按照此示例“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 cman.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

我假设我必须找出用于 windows 的 Visual c++ 的哪些参数才能使 cmean.lib 文件的格式正确。

【问题讨论】:

  • 很确定你不能用 gcc 编译一半,用 MSVC 编译一半。该教程不是在 Windows 上完成的。你应该用 MSVC 编译这两个部分,或者弄清楚如何让 setup.py 使用 gcc。
  • 好的。我还没有找到任何解释如何在 Windows 上执行此操作的地方。必须搜索 Møre。
  • stackoverflow.com/a/2727463/2101267 - 您可能还需要其他编译器选项。也许首先阅读 cython 对 cl.exe 的调用中的内容,如您的帖子中所示。

标签: c external cython


【解决方案1】:

我终于设法通过一个非常简单的修复让它工作。必须使用与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}
    )

--fossekall

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2020-06-02
    • 2015-08-31
    • 2017-02-17
    • 1970-01-01
    相关资源
    最近更新 更多