【问题标题】:Why do I get this warnings under Cython?为什么我会在 Cython 下收到此警告?
【发布时间】:2017-01-20 03:02:04
【问题描述】:

我尝试重现 Cython 教程中的一些示例来学习 Cython:

http://docs.cython.org/en/latest/src/tutorial/external.html

我认为以下两个警告不相关。因此有两个问题:

(1)

使用这个作为输入

python setup.py build_ext --inplace -c mingw32

from libc.math cimport sin

cdef extern from "math.h":
    cdef double sin(double x)


cpdef double f(double x):
    return sin(x*x)

cpdef test(double x):
    return f(x)

我明白了:

D:\python\cython>python setup.py build_ext --inplace -c mingw32
Compiling primes.pyx because it changed.
[1/1] Cythonizing primes.pyx
warning: primes.pyx:4:19: Function signature does not match previous declaration
running build_ext
building 'primes' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python34\include -IC:\Python34\include -c primes.c -o build\temp.win32-3.4\Release\primes.o
writing build\temp.win32-3.4\Release\primes.def
C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-3.4\Release\primes.o build\temp.win32-3.4\Release\primes.def -LC:\Python34\libs -LC:\Python34\PCbuild -lpython34 -lmsvcr100 -o D:\python\cython\primes.pyd

D:\python\cython>

为什么会出现警告“函数签名与之前的声明不匹配”?

(2)

当我声明时

cdef extern from "math.h":
    cpdef double sin(double x)

我收到了额外的警告

warning: primes.pyx:4:20: Function 'sin' previously declared as 'cpdef'

但是,它的给出方式与链接页面的“外部声明”一章中的示例完全相同。在导入模块的python模块中,sin在包下是不知道的。问题出在哪里?

教程中的描述是:

Note that you can easily export an external C function from your Cython module by declaring it as cpdef. This generates a Python wrapper for it and adds it to the module dict. 

【问题讨论】:

  • “我认为以下两个警告不相关。因此两个问题:”每个问题一个问题

标签: cython


【解决方案1】:

本教程的不同部分展示了调用 C 函数的不同方式。

对于某些提供了 Cython .pxd 标头的函数,您可以使用 from libc.math import sin。对于所有的库,你可以使用.h header的更长的方法并重新声明。

但是,您不能将两者混为一谈,因为它会创建同一函数的两个定义,即使它们是相同的。

【讨论】:

  • 我明白了。第二个问题与此有关吗?
  • 很可能 :-) Function 'sin' previously declared as 'cpdef' 也是关于多重定义的名称。无论如何,你应该为自己尝试。只是一个额外的说明:如果你想迭代测试 Cython 编程,你可以使用 Jupyter notebooks docs.cython.org/en/latest/src/quickstart/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多