Cython Cimport unresolved external symbol

ぃ、小莉子 提交于 2020-01-06 03:24:06

问题


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

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