问题
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)
)
and I'm experimenting with cimport to get it to work.
from numpy.math cimport expl
import numpy as np
print(expl(5-2))
However, the erros are
error LNK2001: unresolved external symbol _npy_expl
Any idea? I have checked that my cython/includes/numpy/math.pxd had this:
long double expl "npy_expl"(long double x)
Any ideas?
回答1:
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
.
The usual Numpy-API is header-only, 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.
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
:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy as np
import os
npymath_path = os.path.join(os.path.dirname(np.get_include()), 'lib')
extensions = [
Extension('_hmmc', ['_hmmc.pyx'],
include_dirs = [np.get_include()],
libraries = ['npymath'],
library_dirs=[npymath_path]
),
]
setup(
ext_modules = cythonize(extensions)
)
I'm not aware of a better way to get npymath_path
: there is a function get_mathlibs
from numpy.distutils.misc_util
but it only works if _numpyconfig.h
which is not the case at least in my installation.
来源:https://stackoverflow.com/questions/54960793/cython-cimport-unresolved-external-symbol