在Cython中包装C库:预期')',找到' *'

时间:2018-04-29 13:05:15

标签: cython

我想用以下头文件包装一个库:

#include "rbtree.h"

struct interval_tree_node {
    struct rb_node rb;
    unsigned long start;    /* Start of interval */
    unsigned long last; /* Last location _in_ interval */
    unsigned long __subtree_last;
};

extern void
interval_tree_insert(struct interval_tree_node *node, struct rb_root *root);

extern void
interval_tree_remove(struct interval_tree_node *node, struct rb_root *root);

extern struct interval_tree_node *
interval_tree_iter_first(struct rb_root *root,
             unsigned long start, unsigned long last);

extern struct interval_tree_node *
interval_tree_iter_next(struct interval_tree_node *node,
            unsigned long start, unsigned long last);

这是我的intervaltree.pxd:

cdef extern from "src/intervaltree.h":

    struct interval_tree_node:
        struct rb_node

    void interval_tree_insert(struct interval_tree_node *node, struct rb_root *root)

    void interval_tree_remove(struct interval_tree_node *node, struct rb_root *root)

    struct interval_tree_node * interval_tree_iter_first(struct rb_root *root, unsigned long start, unsigned long last)

    struct interval_tree_node * interval_tree_iter_next(struct interval_tree_node *node, unsigned long start, unsigned long last)

这是我的setup.py:

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

setup(
    ext_modules = cythonize([Extension("intervaltree", ["intervaltree.pyx"])])
)

我的intervaltree.pyx只是空的,或

cimport intervaltree

当我运行python setup.py install时,我收到以下错误:

    Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "src/intervaltree.h":

    struct interval_tree_node:
        struct rb_node

    void interval_tree_insert(struct interval_tree_node *node, struct rb_root *root)
                                                       ^
------------------------------------------------------------

intervaltree.pxd:6:56: Expected ')', found '*'

由于文档Defining external declarations中的示例使用语法

int queue_push_head(Queue* queue, QueueValue data)

不是

int queue_push_head(Queue *queue, QueueValue data)

我试图改变这一部分,但似乎没有帮助。我究竟做错了什么?

1 个答案:

答案 0 :(得分:1)

你错过了cdef前面的struct interval_tree_node,所以Cython没有认出它是一种类型:

cdef struct interval_tree_node
    rb_node rb # assuming you've also defined rb_node somewhere

The relevant documentation

同样,您希望在Cython代码中编写interval_tree_node,而不是struct interval_tree_node

从OP更新。这就是最终看起来的样子:

cdef extern from "src/rbtree.h":

    cdef struct rb_root:
        pass

cdef extern from "src/interval_tree.h":

    cdef struct interval_tree_node:
        pass

    void interval_tree_insert(interval_tree_node *node, rb_root *root)

    void interval_tree_remove(interval_tree_node *node, rb_root *root)

    interval_tree_node * interval_tree_iter_first(rb_root *root, unsigned long start, unsigned long last)

    interval_tree_node * interval_tree_iter_next(interval_tree_node *node, unsigned long start, unsigned long last)