0

I have a problem in a bigger project but I managed to isolate it into a smallest possible example. Please take a look.

The def.c file that contains a definition of a struct

typedef struct {
  unsigned char data[64];
} somestruct;

a build.py file that builds the cffi wrapper

import os

from cffi import FFI, VerificationError

basepath = os.path.abspath(os.path.dirname(__file__))

ffi = FFI()

# the def.c is both header and source
with open(basepath + '/def.c', 'rt') as fid:
    _source = fid.read()
    ffi.cdef(_source)

ffi.set_source(
    "some_examples",
    _source,
    extra_compile_args=['-g']
)

ffi.compile()

the file run.py that attempts to instantiate an empty struct

from some_examples import ffi, lib

ffi.new('struct somestruct *')

How to run

python build.py
python run.py

what I get

Traceback (most recent call last):
  File "run.py", line 3, in <module>
    ffi.new('struct somestruct *')
ffi.error: undefined struct/union name
struct somestruct *
       ^

I researched it a bit, this answer was helpful but it indicated that definition must be in cdef() which is the case here. Anyone has some ideas why is somestruct undefined?

Marek
  • 1,413
  • 2
  • 20
  • 36

0 Answers0