如何用CFFI调用struct

2024-10-01 15:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python文件调用C++ Fixt,它已经通过CFFI翻译了。我当前遇到了结构不可调用的错误。如何正确调用此结构?我是否首先在cffi转换器中正确地定义了结构?你知道吗

(简体)_生成器.py)你知道吗

import os
from cffi import FFI

ffibuilder = FFI()

basePath = os.path.dirname(__file__)
filePath = os.path.abspath(os.path.join(basePath, "..", "..", "build/bin/simple_bfs.cpp"))

runtimePath = os.path.abspath(os.path.join(basePath, "..", "..", "src/runtime_lib"))
intrinsicsPath = os.path.abspath(os.path.join(basePath, "..", "..", "src/runtime_lib/intrinsics.h"))

with open(filePath,'r') as f:
    ffibuilder.set_source("_simplebfs_cffi",
                          f.read(),
                          include_dirs=[runtimePath],
                          source_extension = ".cpp",
                          extra_compile_args = ["-std=c++11"]
)

ffibuilder.cdef(
    """
    struct run_bfs{};
    """
)

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)

我试图调用的结构(\u simplebfs_cffi.cpp公司)地址:

struct run_bfs
{
  void operator() () 
  {
 lots of code stuff
  }
}

< >我用的代码来调用C++代码生成的代码(Run-SimeLeE.BfsS.cffiig)Python.py)地址:

import os
import _simplebfs_cffi
from cffi import FFI

ffi = FFI()

class run_bfs():
    def __init__(self):
        self.bfs = _simplebfs_cffi.ffi.new("struct run_bfs*", None)

    def __call__(self):
        (self.bfs)()

run_bfs()()

当我尝试运行上面的代码时,出现错误:

回溯(最近一次调用last):文件“run\u simple\u bfs\u cffi_Python.py“,第18行,在run\u bfs()()文件“run\u simple\u bfs\u cffi_Python.py,第15行,在呼叫中(自我.bfs)()类型错误:cdata“struct run\u bfs*”不可调用


Tags: 文件pathrunpyimportselfoscffi

热门问题