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?

PuercoPop
  • 6,707
  • 4
  • 30
  • 40
Maximilian
  • 1,325
  • 2
  • 14
  • 35

1 Answers1

1

ffi.cdef() is not capable of handling preprocessor directives. The purpose of ffi.cdef() is to specify objects that are shared between python and C. It is not compiled (this example does not call any C compiler). Either you eliminate all preprocessor directives from your filestream f or you cherrypick those header parts that you actually need and copy-paste them into your ffi.cdef().

dornhe
  • 21
  • 3