Tag: python cffi

使用Python的CFFI并排除系统头

我正在尝试使用Python的CFFI开发Python绑定到用C编写的科学模型.CFFI文档有点稀疏,我陷入了cdef阶段。 到目前为止,我的流程遵循以下步骤: 预处理头文件: gcc -E -gcc -std=c99 -E -P src/my_c_interface.c -I./include/ -I../shared/include/ > header.txt 这将生成一个文本文件,其中包含include/目录中头文件中包含的所有C声明。 它还包括标准库的声明(我很确定这是我的问题所在)。 header.txt看起来像这样(完整的header.txt在这里 ): 从系统标题开始: typedef float float_t; typedef double double_t; extern int __math_errhandling(void); extern int __fpclassifyf(float); extern int __fpclassifyd(double); extern int __fpclassifyl(long double); extern __inline __attribute__((__gnu_inline__)) __attribute__ ((__always_inline__)) int __inline_isfinitef(float); extern __inline __attribute__((__gnu_inline__)) __attribute__ 并以我的标题中定义的片段结束: FILE *LOG_DEST; void finalize_logging(void); void […]

在Python CFFI中声明包含time_t字段的struct

我正在使用CFFI从Python调用一个返回结构的C函数。 结构使用time_t元素定义。 如何将结构声明为CFFI,以便我可以从Python访问它? 例如,我尝试了以下(获取文件的修改时间): import cffi ffi = cffi.FFI() ffi.cdef(“”” // From POSIX struct timespec { time_t tv_sec; long tv_nsec; …; }; struct stat { struct timespec st_mtim; …; }; // From “man 2 lstat” int lstat(const char *path, struct stat *buf); “””) stat = ffi.verify(“#include “) 这给出了一个错误: cffi.api.CDefError: cannot parse ” time_t tv_sec;” :5: […]