2

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.

1 Answer 1

5

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;
Sign up to request clarification or add additional context in comments.

2 Comments

real type width may differ. PLEASE DO NOT DEFINE JUST TO INT
@socketpair: no, the point of the syntax typedef int... off_t;, with the ellipsis, is precisely to ask CFFI to find the real type width for you. It doesn't mean "use exactly int".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.