3

我正在尝试使用Python 的 CFFI开发 Python 绑定到用 C 编写的科学模型。CFFI 文档有点稀疏,我被困在了这个cdef阶段。

到目前为止,我的流程遵循以下步骤:

  1. 预处理头文件:

    gcc -E -gcc -std=c99 -E -P src/my_c_interface.c -I./include/ -I../shared/include/ > header.txt

    include/这将生成一个文本文件,其中包含我的目录中的头文件中包含的所有 C 声明。它还包括标准库的声明(我很确定这是我的问题所在)。header.txt看起来像这样(完整的 header.txt 在这里

    从系统头文件开始:

    typedef float float_t;
    typedef double double_t;
    extern int __math_errhandling(void);
    extern int __fpclassifyf(float);
    extern int __fpclassifyd(double);
    extern int __fpclassifyl(long double);
    extern __inline __attribute__((__gnu_inline__)) __attribute__ ((__always_inline__)) int __inline_isfinitef(float);
    extern __inline __attribute__((__gnu_inline__)) __attribute__
    

    并以我的标题中定义的部分结束:

    FILE *LOG_DEST;
    void finalize_logging(void);
    void get_current_datetime(char *cdt);
    void get_logname(const char *path, int id, char *filename);
    
  2. 用于cffi解析预处理的头文件:

    import cffi
    
    ffi = cffi.FFI()
    
    with open('drivers/python/header.txt') as f_headers:
        ffi.cdef(f_headers.read())  # error is raised here
    
    ffi.compile()
    

    这将返回以下错误(完整的回溯在这里):

    /Users/me/anaconda/lib/python3.4/site-packages/cffi/cparser.py in convert_pycparser_error(self, e, csource)
        157         else:
        158             msg = 'parse error\n%s' % (msg,)
    --> 159         raise api.CDefError(msg)
        160 
        161     def parse(self, csource, override=False, packed=False):
    
    CDefError: cannot parse "extern __inline __attribute__((__gnu_inline__)) __attribute__ ((__always_inline__)) int __inline_isfinitef(float);"
    :10:17: before: __attribute_
    

鉴于我所处的位置,对于那些比我更熟悉 cffi 的人,我有几个问题:

  1. 是否可以让预处理器排除系统头文件?
  2. cffi有人知道比文档中显示的更复杂的任何示例吗?现实世界的例子会有所帮助。
  3. 看看我上面显示的例子,我错过了什么重要的东西吗?
4

1 回答 1

3

这是一个有点笼统的答案:

虽然可以使用该gcc -E方法并手动“修剪”结果,但这不是使用 CFFI 的推荐方法。相反,cdef() 代码通常是增量生成的(根据需要添加函数)或从 .h 文件的编辑副本中批量生成。从手册页复制时,第一种方法效果最好;第二种方法适用于我们希望完全访问单个 3rd 方库的情况。

在所有情况下,您很可能无论如何都需要编辑 .h 文件:推荐的方法是使用 ffi.set_source(),并从 cdef() 中删除任何多余的声明,将它们替换为 .h 文件...。例如,实际的 .h 文件可能包含声明#define FOOBAR 42,但不应依赖值 42 (例如,将来可能会更改),因此 cdef() 应该接收#define FOOBAR ...

于 2015-08-04T09:08:17.273 回答