CythonGSL/ Using GSL on Windows via Cython

妖精的绣舞 提交于 2021-02-08 07:51:47

问题


Machine Configuration: My config is windows 7 x64, with python 2.7 and cython 0.18 (all 64 bit ) installed. I also have MS C++ 2008 Visual Studio installed.

I have the GSL binaries and have Path pointed to the GSL\Bin.

I am using MS VS 2008 + SDK 7.0 to compile cython .pyx files. I use the SDK 7.0 command prompt to compile to c and cython.

Problem However at compilation time I get a LINK error 2019, which indicates the header files in the GSL folder are not being found .

My Attempts To resolve this I downloaded CythonGSL. using "from cythonGSL cimport *" didnt work. I went to the init file for this package and changed the directories it appears to be searching for to fit where I have installed GSL. I have updated the setup.py as shown on the CythonGSL readme.

This hasnt worked either!! At compile time, it says gsl.lib not found.

Any idea how i can fix this?

One option is if I could point a static address for example, in place of

    cdef extern from "libcalg/queue.h":
       with
    cdef extern from "FUll File Path/queue.h":

That doesnt work for now.

setup.py

from distutils.core import setup
from Cython.Distutils import Extension
from Cython.Distutils import build_ext
import cython_gsl
import numpy as np
setup(

    include_dirs = [cython_gsl.get_include(), np.get_include()],
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("lda_gibbs_cython",
                             ["lda_gibbs_cython.pyx"],
                             libraries=cython_gsl.get_libraries(),
                             library_dirs=[cython_gsl.get_library_dir()],
                             include_dirs=[cython_gsl.get_cython_include_dir()])]
    )

The only difference is I include np.get _include()

My .pyx file : (borrowed for testing from https://github.com/twiecki/CythonGSL/blob/master/examples/gibbs.pyx )

import numpy as np
cimport numpy as np
cimport cython
DTYPE = np.int
ctypedef np.int_t DTYPE_t
from scipy.special import gammaln
from cython_gsl cimport *

cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)

Error message from the Cython GSL attempt:

D:\LDA\pythonLDA>setup.py build_ext --inplace
running build_ext
cythoning lda_gibbs_cython.pyx to lda_gibbs_cython.c
building 'lda_gibbs_cython' extension
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\Bin\amd64\cl.exe /c /nolog
o /Ox /MD /W3 /GS- /DNDEBUG -IC:\Python27\lib\site-packages -IC:/GSL/include -IC
:\Python27\lib\site-packages\numpy\core\include -IC:\Python27\include -IC:\Pytho
n27\PC /Tclda_gibbs_cython.c /Fobuild\temp.win-amd64-2.7\Release\lda_gibbs_cytho
n.obj
lda_gibbs_cython.c
c:\python27\lib\site-packages\numpy\core\include\numpy\npy_deprecated_api.h(8) :
 Warning Msg: Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECA
TED_API NPY_1_7_API_VERSION
lda_gibbs_cython.c(2812) : warning C4244: 'function' : conversion from '__int64'
 to 'long', possible loss of data
lda_gibbs_cython.c(4811) : warning C4101: 'getbuffer_cobj' : unreferenced local
variable
lda_gibbs_cython.c(4842) : warning C4101: 'releasebuffer_cobj' : unreferenced lo
cal variable
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\Bin\amd64\link.exe /DLL /n
ologo /INCREMENTAL:NO /LIBPATH:C:/GSL/lib /LIBPATH:C:\Python27\libs /LIBPATH:C:\
Python27\PCbuild\amd64 gsl.lib gslcblas.lib /EXPORT:initlda_gibbs_cython build\t
emp.win-amd64-2.7\Release\lda_gibbs_cython.obj /OUT:D:\LDA\pythonLDA\lda_
gibbs_cython.pyd /IMPLIB:build\temp.win-amd64-2.7\Release\lda_gibbs_cython.lib /
MANIFESTFILE:build\temp.win-amd64-2.7\Release\lda_gibbs_cython.pyd.manifest /MAN
IFEST
LINK : fatal error LNK1181: cannot open input file 'gsl.lib'
error: command 'link.exe' failed with exit status 1181

GCC Attempt

I installed TDM-GCC so i could use the standard cmd window:

.pyx, and setup.py remain the same.I had an issue documented and resolved here -mno-cygwin : gcc building Mercurial (Windows 2000) . Still no cigar. I dont know if this should be a separate question - will move if the experts think so.

D:\LDA\pythonLDA>setup.py build_ext --inplace -c mingw32
running build_ext
skipping 'lda_gibbs_cython.c' Cython extension (up-to-date)
building 'lda_gibbs_cython' extension
C:\MinGW64\bin\gcc.exe -mdll -O -Wall -IC:\Python27\lib\site-packages -IC:/GSL/i
nclude -IC:\Python27\lib\site-packages\numpy\core\include -IC:\Python27\include
-IC:\Python27\PC -c lda_gibbs_cython.c -o build\temp.win-amd64-2.7\Release\lda_g
ibbs_cython.o
writing build\temp.win-amd64-2.7\Release\lda_gibbs_cython.def
C:\MinGW64\bin\gcc.exe -shared -s build\temp.win-amd64-2.7\Release\lda_gibbs_cyt
hon.o build\temp.win-amd64-2.7\Release\lda_gibbs_cython.def -LC:/GSL/lib -LC:\Py
thon27\libs -LC:\Python27\PCbuild\amd64 -lgsl -lgslcblas -lpython27 -l -o D:\LDA\pythonLDA\lda_gibbs_cython.pyd
gcc: error: D:\LDA\pythonLDA\lda_gibbs_cython.pyd: No such file or direct
ory
error: command 'gcc' failed with exit status 1

回答1:


I only used CythonGSL - the current version (https://github.com/twiecki/CythonGSL/blob/master/cython_gsl/__init__.py) reads environmental variable LIB_GSL to determine the location of GSL directory. Make sure you have LIB_GSL environmental variable to point your main GSL folder (not \bin folder). For example, LIB_GSL=C:/GSL. And then you have to add C:\GSL\bin to your PATH. I have a blog post with detailed explanation.

More explanations from Chat Discussion: TDM-GCC + EPD + GSL (with a minor change - GSL files have to be at C:\Program Files\GnuWin32\. This works perfectly for 64 bit windows 7.

Using the Windows binaries which were probably compiled on MS VS C++ 2008 could lead to conflict with mingw64 compiled C++ programs.


More explanation of the setup (Added)

In general, it is much easier to use a scientific python distribution such as Python(x,y) (Free), Enthought Canopy (successor of EPD, limited Free version), or Anaconda (Free except advanced packages) than to install individual packages on top of vanilla python. It seems all three distributions come with MinGW, so you should be good to if you don't need openmp support for Cython, and if you do, you can install TDM-GCC.

In terms of GSL, you can download binary GSL from oscats projects. It has both 32 and 64 bit binaries with devel headers.

My setup is Anaconda + TDM-GCC + GSL (from oscats projects) and it has been working very well.



来源:https://stackoverflow.com/questions/15910259/cythongsl-using-gsl-on-windows-via-cython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!