Search code examples
pythonpython-cffi

python cffi parsing error when defining a struct


I am trying to use python-cffi to wrap C code. The following example_build.py shows an attempt to wrap lstat() call:

import cffi

ffi = cffi.FFI()
ffi.set_source("_cstat",
        """
        #include <sys/types.h>
        #include <sys/stat.h>
        #include <unistd.h>
        """,
        libraries=[])

ffi.cdef("""
        struct stat {
            mode_t  st_mode;
            off_t   st_size;
            ...;
        };
        int lstat(const char *path, struct stat *buf);
""")


if __name__ == '__main__':
    ffi.compile()

When compile python example_build.py will complain that parsing error for mode_t st_mode.

cffi.api.CDefError: cannot parse "mode_t  st_mode;"
:4:13: before: mode_t

A similar example given from the manual doesn't have any problems though. What am I missing? TIA.


Solution

  • You need to inform CFFI that mode_t and off_t are some integer types. The easiest way is to add these lines first in the cdef():

    typedef int... mode_t;   /* means "mode_t is some integer type" */
    typedef int... off_t;