2

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.

Armali
  • 18,255
  • 14
  • 57
  • 171
fossekall
  • 521
  • 1
  • 10
  • 27
  • Pretty sure you can't compile half of it with gcc and half with MSVC. That tutorial was not done on Windows. You should compile both parts with MSVC or figure out how to make setup.py use gcc. – Dark Falcon Dec 22 '15 at 22:16
  • Okay. I have not found any place who explain how to do this on Windows. Have to search Møre. – fossekall Dec 22 '15 at 22:50
  • http://stackoverflow.com/a/2727463/2101267 - You may need other compiler options also. Perhaps start by reading up on the ones in the call made to `cl.exe` by cython as shown in your post. – Dark Falcon Dec 23 '15 at 01:23

1 Answers1

0

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

Armali
  • 18,255
  • 14
  • 57
  • 171