3
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')

The code above is the one I am actually struggling with. For using some functions of libelf, I need to wrap the library and the header. After long time of recherche this seems to be the right approach to do that.

But I get a parsing error:

cannot parse "#ifndef _LIBELF_H"

It seems that all kinds these expressions cause parsing errors. How can I solve this problem? Or is there another approach of wrapping both: library and header?

4

1 回答 1

0

ffi.cdef() 无法处理预处理器指令。ffi.cdef() 的目的是指定在 python 和 C 之间共享的对象。它没有被编译(这个例子没有调用任何 C 编译器)。要么从文件流中删除所有预处理器指令,要么f挑选你实际需要的那些标题部分并将它们复制粘贴到你的 ffi.cdef() 中。

于 2021-05-17T14:54:21.513 回答