消除Cython numpy编译警告的方法?

时间:2014-03-01 05:16:48

标签: python c numpy cython

我遇到了这里描述的问题(What is this import_umath function?)并想知道是否有修复方法?我有完全相同的情况,编译使用numpy的Cython代码与以下代码:

import numpy as np
cimport numpy as np
np.import_array()

生成许多关于_import_umath未被使用的警告:

/usr/local/lib/python2.7/dist-packages/numpy-1.6.2-py2.7-linux-x86_64.egg/numpy/core/include/numpy/__ufunc_api.h:226:1: warning: ‘_import_umath’ defined but not used [-Wunused-function]

删除np.import_array()不会更改结果。就像上面帖子中建议的一个海报一样,我尝试在我的.pxd/.pyx文件中添加它:

cdef extern from *:
    import_umath()

这也没有区别。如何消除这种警告?

2 个答案:

答案 0 :(得分:4)

您可以使用extra_compile_args中的关键字setup.py将参数传递给C编译器。例如,这不会生成警告:

from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
import numpy

extensions=[
    Extension("abc",
             ["abc.pyx"],
             include_dirs=[numpy.get_include()],
             extra_compile_args=["-w"]
            )
]

setup(
    ext_modules=cythonize(extensions),
)

答案 1 :(得分:1)

Cython Tricks and Tips中,他们解释说您需要:

cdef extern from *:
    pass

导入extern包时。 I already needed this trick to write a wrapper,也许它也适合你......