【问题标题】:How to properly wrap a C library with Python CFFI如何使用 Python CFFI 正确包装 C 库
【发布时间】:2016-12-23 03:02:18
【问题描述】:

我正在尝试包装一个非常简单的 C 库,其中只包含两个 .C 源文件:dbc2dbf.cblast.c

我正在执行以下操作(来自文档):

import os
from cffi import FFI
blastbuilder = FFI()
ffibuilder = FFI()
with open(os.path.join(os.path.dirname(__file__), "c-src/blast.c")) as f:
    blastbuilder.set_source("blast", f.read(), libraries=["c"])
with open(os.path.join(os.path.dirname(__file__), "c-src/blast.h")) as f:
    blastbuilder.cdef(f.read())
blastbuilder.compile(verbose=True)

with open('c-src/dbc2dbf.c','r') as f:
    ffibuilder.set_source("_readdbc",
                          f.read(),
                          libraries=["c"])

with open(os.path.join(os.path.dirname(__file__), "c-src/blast.h")) as f:
    ffibuilder.cdef(f.read(), override=True)

if __name__ == "__main__":
    # ffibuilder.include(blastbuilder)
    ffibuilder.compile(verbose=True)

这不是很有效。我认为我没有正确包含 blast.c

谁能帮忙?

【问题讨论】:

  • 不确定为什么需要两个FFI() 实例。这当然不在文档中...

标签: python c python-cffi


【解决方案1】:

这是解决方案(已测试):

import os
from cffi import FFI

ffibuilder = FFI()

PATH = os.path.dirname(__file__)

with open(os.path.join(PATH, 'c-src/dbc2dbf.c'),'r') as f:
    ffibuilder.set_source("_readdbc",
                          f.read(),
                          libraries=["c"],
                          sources=[os.path.join(PATH, "c-src/blast.c")],
                          include_dirs=[os.path.join(PATH, "c-src/")]
                          )
ffibuilder.cdef(
    """
    static unsigned inf(void *how, unsigned char **buf);
    static int outf(void *how, unsigned char *buf, unsigned len);
    void dbc2dbf(char** input_file, char** output_file);
    """
)

with open(os.path.join(PATH, "c-src/blast.h")) as f:
    ffibuilder.cdef(f.read(), override=True)

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)

【讨论】:

  • 我认为我仍然可以摆脱 'blast.h' 上的 cdef,因为我不需要从那里公开任何函数。
  • 我应该提到,如果没有@armin-rigo 的帮助,我将找不到这个解决方案!再次感谢!
猜你喜欢
  • 2020-09-13
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 2021-10-17
  • 2015-03-31
  • 2021-08-06
  • 2014-08-29
相关资源
最近更新 更多