python - CythonGSL/通过 Cython 在 Windows 上使用 GSL

标签 python windows cython gsl

机器配置: 我的配置是 windows 7 x64,安装了 python 2.7 和 cython 0.18(所有 64 位)。我还安装了 MS C++ 2008 Visual Studio。

我有 GSL 二进制文件并且 Path 指向 GSL\Bin。

我正在使用 MS VS 2008 + SDK 7.0 编译 cython .pyx 文件。我使用 SDK 7.0 命令提示编译为 c 和 cython。

问题 但是在编译时我收到一个链接错误 2019,这表明找不到 GSL 文件夹中的头文件。

我的尝试 为了解决这个问题,我下载了 CythonGSL。使用“来自 cythonGSL cimport *”没有用。我转到这个包的 init 文件并更改了它似乎正在搜索的目录以适合我安装 GSL 的位置。 我已经更新了 setup.py,如 CythonGSL 自述文件中所示。

也没有用!! 在编译时,它说找不到 gsl.lib。

知道如何解决这个问题吗?

一个选项是,如果我可以指向一个静态地址,例如,代替

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

这暂时不起作用。

设置.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()])]
    )

唯一的区别是我包含了 np.get _include()

我的 .pyx 文件:(从 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)

Cython GSL 尝试的错误消息:

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 尝试

我安装了 TDM-GCC,所以我可以使用标准的 cmd 窗口:

.pyx 和 setup.py 保持不变。我在此处记录并解决了一个问题 -mno-cygwin : gcc building Mercurial (Windows 2000) .仍然没有雪茄。我不知道这是否应该是一个单独的问题 - 如果专家这么认为,将会提出。

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

最佳答案

我只使用了 CythonGSL - 当前版本 ( https://github.com/twiecki/CythonGSL/blob/master/cython_gsl/__init__.py ) 读取环境变量 LIB_GSL 来确定 GSL 目录的位置。确保你有 LIB_GSL 环境变量来指向你的主 GSL 文件夹(不是 \bin 文件夹)。例如,LIB_GSL=C:/GSL。然后您必须将 C:\GSL\bin 添加到您的 PATH。我有一个 blog post有详细的解释。

聊天讨论中的更多解释: TDM-GCC + EPD + GSL(稍作改动 - GSL 文件必须位于 C:\Program Files\GnuWin32\。这非常适合 64 位 Windows 7。

使用可能在 MS VS C++ 2008 上编译的 Windows 二进制文件可能会导致与 mingw64 编译的 C++ 程序发生冲突。


设置的更多说明(已添加)

一般来说,使用像Python(x,y)这样的科学python发行版要容易得多。 (免费),Enthought Canopy (EPD 的继任者,有限免费版),或 Anaconda (除了高级包之外免费)而不是在 vanilla python 之上安装单独的包。似乎所有三个发行版都带有 MinGW,所以如果您不需要对 Cython 的 openmp 支持,您应该很好,如果需要,您可以安装 TDM-GCC .

GSL而言,您可以从oscats下载二进制GSL项目。它具有带有开发 header 的 32 位和 64 位二进制文​​件。

我的设置是 Anaconda + TDM-GCC + GSL(来自 oscats 项目)并且运行良好。

关于python - CythonGSL/通过 Cython 在 Windows 上使用 GSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15910259/

相关文章:

python - 从 python 连接到 sybase 数据库的最佳方法是什么?

python - tkinter 中主框架上的框架布局

javascript - 如何从命令提示符在浏览器中执行 JavaScript

windows - 使用 Powershell 创建的本地用户帐户未显示在设置 "Family & Other people"中

python - Cython/Pyrex 生成的 Python 扩展是否线程安全?

python - `cimport numpy` 使用 Cython 引发错误

python - 打印字符串在 Python 中的字符串之前打印 'u'?

python - 在 python pandas 中将时间对象转换为日期时间格式

php - 如何修复 PHP 警告 : file_get_contents?

python - 如何在 cython 中声明列表列表