Cython Cimport无法解析的外部符号 [英] Cython Cimport unresolved external symbol
问题描述
我的setup.py是:
My setup.py are :
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy as np
extensions = [
Extension('_hmmc', ['_hmmc.pyx'], include_dirs = [np.get_include()]),
]
setup(
ext_modules = cythonize(extensions)
)
,而我正在尝试使用cimport使其正常工作。
and I'm experimenting with cimport to get it to work.
from numpy.math cimport expl
import numpy as np
print(expl(5-2))
但是,错误是
错误LNK2001:无法解析的外部符号_npy_expl
error LNK2001: unresolved external symbol _npy_expl
有什么想法吗?我检查了我的cython / includes / numpy / math.pxd是否具有以下内容:
Any idea? I have checked that my cython/includes/numpy/math.pxd had this:
long double expl npy_expl(long double x)
long double expl "npy_expl"(long double x)
有什么想法吗?
推荐答案
可能为了简单起见,可以使用标准库中的 exp
,否则会有一些麻烦,以使其与 npy_expl一起使用
。
Probably to keep it simple, one could use exp
from the standard library, otherwise there are some hoops to jump through, to get it work with npy_expl
.
通常的Numpy-API仅用于标头(更准确地说,在编译/链接时仅需要标头,请参见此处),但数学函数并非如此。有定义 NPY_INLINE_MATH
,它也可以将numpy的数学库显示为内联函数,但这不适用于已安装的numpy-distributionen,因为它们缺少 core / src
文件夹,其中给出了数学函数的定义。
The usual Numpy-API is header-only (more precisely, only headers are needed at compile-/linktime, see here), but this is not the case with math-functions. There is define NPY_INLINE_MATH
, which would also present numpy's math library as inline functions, but this will not work on installed numpy-distributionen, because those lack the core/src
-folder, where the definitions of math-functions are given.
因此,您必须添加预编译的静态numpy的数学库设置。可以在文件夹 core / lib
中找到它,并且至少在Linux上称为 libnpymath.a
。
So you have to add the precompiled static numpy's math library to your setup. It can be found in folder core/lib
and is called (at least on linux) libnpymath.a
.
最可靠的方法是使用 numpy.distutils.misc_util $ c中的
get_info
$ c>,返回一个字典,该字典提供'define_macros'
,
'include_dirs'
,'libraries'
和'library_dirs'
-即我们需要传递给扩展的值,例如
The most robust way is to use get_info
from numpy.distutils.misc_util
, which returns a dictionary providing values for 'define_macros'
,
'include_dirs'
, 'libraries'
and 'library_dirs'
- i.e. values we need to pass to Extension, e.g.
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from numpy.distutils.misc_util import get_info
npymath_info = get_info('npymath')
extensions = [
Extension('_hmmc', ['_hmmc.pyx'],
**npymath_info
),
]
setup(
ext_modules = cythonize(extensions)
)
还有函数 get_mathlibs $ c来自
numpy.distutils.misc_util
的$ c>,但仅当存在 _numpyconfig.h
时才起作用,而不是至少在我的安装中。
There is also the function get_mathlibs
from numpy.distutils.misc_util
but it only works if _numpyconfig.h
is present, which is not the case at least in my installation.
这篇关于Cython Cimport无法解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!