1

我正在尝试使用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()

编译python example_build.py时会抱怨mode_t st_mode.

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

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

4

1 回答 1

5

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

typedef int... mode_t;   /* means "mode_t is some integer type" */
typedef int... off_t;
于 2015-10-21T22:23:39.247 回答