【问题标题】:How do I wrap a C-library including its header into a python program using CFFI?如何使用 CFFI 将包含其标头的 C 库包装到 python 程序中?
【发布时间】:2015-10-05 20:26:59
【问题描述】:
from cffi import FFI
ffi = FFI()
header_path = '/usr/include/libelf.h'
with open(header_path) as f:
      ffi.cdef(f.read())
lib = ffi.dlopen('/usr/local/lib/libelf.so')

上面的代码是我真正遇到的问题。为了使用 libelf 的一些功能,我需要包装库和头文件。经过长时间的研究,这似乎是做到这一点的正确方法。

但是我得到一个解析错误:

无法解析“#ifndef _LIBELF_H”

似乎所有这些表达式都会导致解析错误。我怎么解决这个问题?还是有另一种包装两者的方法:库和标题?

【问题讨论】:

    标签: python parsing wrapper elf python-cffi


    【解决方案1】:

    ffi.cdef() 不能处理预处理器指令。 ffi.cdef() 的目的是指定在 python 和 C 之间共享的对象。它没有被编译 (this example does not call any C compiler)。要么从文件流f 中删除所有预处理器指令,要么挑选你真正需要的那些标题部分并将它们复制粘贴到你的 ffi.cdef() 中。

    【讨论】:

    • 刚才我明白了,我的答案来得太晚了~5 年。干杯。
    猜你喜欢
    • 2016-12-23
    • 2013-06-15
    • 2020-09-13
    • 1970-01-01
    • 2020-03-30
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多