定义结构时python cffi解析错误

pyt*_*152 1 python python-cffi

我试图用来python-cffi包装C代码.以下example_build.py显示了包装lstat()调用的尝试:

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()
Run Code Online (Sandbox Code Playgroud)

当编译时python example_build.py会抱怨解析错误mode_t st_mode.

cffi.api.CDefError: cannot parse "mode_t  st_mode;"
:4:13: before: mode_t
Run Code Online (Sandbox Code Playgroud)

手册中给出的类似示例没有任何问题.我错过了什么?TIA.

Arm*_*igo 5

您需要通知CFFI mode_t并且off_t是一些整数类型.最简单的方法是首先在cdef()中添加这些行:

typedef int... mode_t;   /* means "mode_t is some integer type" */
typedef int... off_t;
Run Code Online (Sandbox Code Playgroud)

  • @socketpair:不,语法`typedef int ... off_t;`的省略号,正是要求CFFI为你找到真正的类型宽度.它并不意味着"使用完全int". (6认同)