简体   繁体   中英

Cython: LINK : fatal error LNK1181: cannot open input file

Have tried to follow this example " Python extensions with C libraries made easy by Cython " but I do not get it to work. My system works with normal Cython. With some small changes in setup.py (have to use setuptools instead of distutils since I am using Windows). Have made the following files:

* 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}
    )

Compile the cmean.c with this command:

gcc  -shared cmean.c -o libcmean.so

But when I run:

python setup.py build_ext --inplace

I get these error messages:

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 

I have followed the example as good as I can.

UPDATE I have made a cmean.lib file as the last error message said it did not find with the help of Microsoft Visual Studio 2008 x86 tools. Tried to use the same flags as cython have used. I have tried to read a lot abotu what this flags means, but I find a lot of it very technical:

cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG cmean.c

And then making the lib file with:

lib.exe lib.exe /out:cmean cmean.obj

But no I got this error:

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

I assume that I have to find out which arguments to use for visual c++ for windows in order to make the cmean.lib file in a correct format.

I finally managed to get it to work by a very simple fix. Have to use the same name of the library as the pyx file:

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

This as also stated in this post:

Cython compiled C extension: ImportError: dynamic module does not define init function

I also found that I can compile the cmean.c file with setuptools:

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM