【问题标题】:Cython error: declaration does not declare anythingCython 错误:声明未声明任何内容
【发布时间】:2014-11-24 02:55:18
【问题描述】:

我正在编写一些 cython 代码,但遇到了一个奇怪的问题。当我尝试将对象直接从 python 传递到 C 作为结构时,cython 生成代码很好,但 gcc 不喜欢代码输出并给我以下错误:error: declaration does not declare anything。这是我的测试代码:

// cake.h
using Cake = struct CakeStruct {
    int a, b, c;
};

void bake(Cake batter);

和赛通:

# test.pyx

cdef extern from "cake.h":
    void bake(Cake batter)
    ctypedef struct Cake:
        int a
        int b
        int c

def make_one(batter):
    cdef Cake more_batter;
    more_batter.a = 5
    more_batter.b = 10
    print(more_batter.a + more_batter.b)

    bake(more_batter)
    bake(batter)  # <- this line generates bad code

如果查看生成的代码,坏行如下所示:

...

Cake; // this is the error
static Cake __pyx_convert__from_py_Cake(PyObject *);

...

我直接使用 Anaconda 的 cython 0.21 和 Ubuntu 14.04 附带的 gcc 4.8.2。 Cython 代码使用cython --cplus test.pyx 生成,语法检查:

gcc -std=c++11 -fsyntax-only -I`...python include dir...` test.cpp

--

谁能告诉我我在 .pyx 文件中做错了什么?或者这是我绊倒的 cython 错误?

【问题讨论】:

    标签: python c++ cython


    【解决方案1】:

    是的,我认为你是对的,这是 Cython 0.21 中的一个错误。首先,使用 Cython 0.20 进行测试(因为这是我的 linux 发行版所提供的),它给了我

    cake.pyx:16:15: Cannot convert Python object to 'Cake'
    

    我想这是因为转换功能在 0.20 中丢失或不完整,尽管我在发行说明中找不到任何关于此的内容 (https://github.com/cython/cython/blob/master/CHANGES.rst)。

    接下来,我使用 Github 存储库 (https://github.com/cython/cython) 中的 master 分支进行了测试,这非常有效,您提供的 cythongcc 命令没有错误。使用 0.21 版本,我可以重现您看到的错误。

    运行git bisect 表明该错误似乎已在提交fd56551 中修复。这是在 0.21.1(此时的最新版本)之后,因此升级到此版本不会修复错误。看来您将不得不使用开发分支或等待下一个 Cython 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2018-04-07
      相关资源
      最近更新 更多