1

我正在使用 python 文件来调用已经通过 cffi 翻译的 C++ 结构。我目前遇到的错误是我的结构不可调用。我怎样才能正确调用这个结构?我首先正确地对 cffi 翻译器中的结构进行 cdef 吗?

(simple_cffi_builder.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)

我试图调用的结构(_simplebfs_cffi.cpp):

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

我用来调用生成的 C++ 代码的代码 (run_simple_bfs_cffi_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()()

当我尝试运行上面的代码时,我得到了错误:

回溯(最后一次调用):文件“run_simple_bfs_cffi_python.py”,第 18 行,在 run_bfs()() 文件“run_simple_bfs_cffi_python.py”,第 15 行,调用中 (self.bfs)() TypeError: cdata 'struct run_bfs * ' 不可调用

4

0 回答 0