Make .lib file for cython with Visual C++ for Python

617 views
Skip to first unread message

havsula

unread,
Dec 23, 2015, 4:52:30 AM12/23/15
to cython-users
Hello
I have some c-code I want to include in Python by using Cython. Name of the c-file is cmean.c. I also have a cmean.h which define the function in cmean.c.
Have the following c-wrapper file:

# 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



And this setup.py
from setuptools import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext

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

setup(
    name = "Demos",
    ext_modules = cythonize(ext_modules),
    cmdclass = {'build_ext':build_ext}
    )

But I need to make the cmean.c into a library which Cython can use. I have used the following commands
cl.exe /c cmean.c
lib.exe /out:cmean.lib cmean.obj
This makes a cmean.lib file but when I try to run setup.py I got the following error:

E:\GD\UD\Software\CythonTest\calling-c\test2>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 /Tccfib.c /Fobuild\temp.win32-2.7\Release\cfib.obj
cfib.c
creating build\lib.win32-2.7
C:\Users\trofl\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Anaconda2\libs /LIBPATH:C:\Anaconda2\PCbuild /LIBPATH:C:\Anaconda2\PC\VS9.0 /EXPORT:initlib build\temp.win32-2.7\Release\cfib.obj /OUT:build\lib.win32-2.7\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

So there is something I do wrong when making the .lib file.

I have no trouble compiling .pyx files.


havsula

unread,
Dec 26, 2015, 4:32:05 PM12/26/15
to cython-users
SOLVED
After a lot of googling I found that setting the library name equal to the .pyx file name did the trick:

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

Do not understand why this helps, but it works!!!

Chris Barker

unread,
Dec 28, 2015, 11:58:05 AM12/28/15
to cython-users
chances are you would be better off simply compiling your c file in with your extension.

you only want tp build a lib if it's large, and you want to be able to access it from multiple extension files. if your extension is a wrapper around a single, simple C file, then just build it all as one. Something like:

ext_modules=[Extension("lib",
    sources = ["m.pyx", "cmean.c"]
    ) 
]

simple, eh? :-)


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

Robert Bradshaw

unread,
Dec 30, 2015, 5:37:17 PM12/30/15
to cython...@googlegroups.com
On Sat, Dec 26, 2015 at 2:34 AM, havsula <tro...@gmail.com> wrote:
> SOLVED
> After a lot of googling I found that setting the library name equal to the
> .pyx file name did the trick:

Yes, just like Python, the module "m" is in a file named "m.py" or "m.pyx"
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages