Python PYPY,CFFI导入错误CFFI库'_堆i';没有名为';的函数、常量或全局变量;initQueue';

Python PYPY,CFFI导入错误CFFI库'_堆i';没有名为';的函数、常量或全局变量;initQueue';,python,python-2.7,shared-libraries,pypy,python-cffi,Python,Python 2.7,Shared Libraries,Pypy,Python Cffi,所以我尝试使用cffi在pypy中快速访问c库 我将macpro与命令行工具9.1一起使用 具体来说,我比较的是一个项目的纯python优先级队列、heapq、cffi和ctypes 我从roman10的网站上获得了一个优先级队列的C实现代码 我正在遵循cffi的文档,但在尝试调用库中的函数时出错 我有5个文件:用于优先级队列的.c/.hcheap.ccheap.h,带有cffiheap\u inter.py的接口python文件,用于程序访问队列的包装器heap.py,以及测试脚本test\u

所以我尝试使用cffi在pypy中快速访问c库

我将macpro与命令行工具9.1一起使用

具体来说,我比较的是一个项目的纯python优先级队列、heapq、cffi和ctypes

我从roman10的网站上获得了一个优先级队列的C实现代码

我正在遵循cffi的文档,但在尝试调用库中的函数时出错

我有5个文件:用于优先级队列的.c/.h
cheap.c
cheap.h
,带有cffi
heap\u inter.py
的接口python文件,用于程序访问队列的包装器
heap.py
,以及测试脚本
test\u heap.py

from _heap_i import ffi, lib

infTime = 0

# /* create heap */                                                                                                                                                                                                                                                                             

def init():
    #global infTime                                                                                                                                                                                                                                                                             
    #infTime = int(engine.infTime) + 1                                                                                                                                                                                                                                                          
    cheap = ffi.new("struct PQ *")
    lib.initQueue(cheap,nn)
    return cheap

def push(arr, element):
    hn = ffi.new("struct heapNode")
    value = element["time"]
    hn.value = value

    rx = ffi.new("char[]", element["rx"])
    hn.data.rx = rx
    tx = ffi.new("char[]", element["tx"])
    hn.data.tx = tx
    txID = ffi.new("int", element["txID"])
    hn.data.txID = txID
    rxID = ffi.new("int", element["rxID"])
    hn.data.rxID = rxID
    name = ffi.new("int", element["name"])
    hn.data.name = name

    hn.data.time = value

    result = lib.enqueue(hn, arr)

def pop(arr):
    hn = lib.dequeue(arr)
    element = {"time": hn.value,
               "rx" : hn.data.rx,
               "tx" : hn.data.tx,
               "rxID" : hn.data.rxID,
               "txID" : hn.data.txID,
               "name" : hn.data.name,
               "data" : hn.data.data,
               }
    return element

def annihilate(arr, event):
    pass

def peak(arr):
    hn = lib.peak(arr)
    element = {"time": hn.value,
               "rx" : hn.data.rx,
               "tx" : hn.data.tx,
               "rxID" : hn.data.rxID,
               "txID" : hn.data.txID,
               "name" : hn.data.name,
               "data" : hn.data.data,
               }
    return element


def isEvent(arr):
    if arr.size:
        return 1
    else:
        return None

def size(arr):
    return arr.size
这就是错误:我尝试了
export CC=gcc
export CC=clang
以及我的mpich

pypy测试\u heap.py

from _heap_i import ffi, lib

infTime = 0

# /* create heap */                                                                                                                                                                                                                                                                             

def init():
    #global infTime                                                                                                                                                                                                                                                                             
    #infTime = int(engine.infTime) + 1                                                                                                                                                                                                                                                          
    cheap = ffi.new("struct PQ *")
    lib.initQueue(cheap,nn)
    return cheap

def push(arr, element):
    hn = ffi.new("struct heapNode")
    value = element["time"]
    hn.value = value

    rx = ffi.new("char[]", element["rx"])
    hn.data.rx = rx
    tx = ffi.new("char[]", element["tx"])
    hn.data.tx = tx
    txID = ffi.new("int", element["txID"])
    hn.data.txID = txID
    rxID = ffi.new("int", element["rxID"])
    hn.data.rxID = rxID
    name = ffi.new("int", element["name"])
    hn.data.name = name

    hn.data.time = value

    result = lib.enqueue(hn, arr)

def pop(arr):
    hn = lib.dequeue(arr)
    element = {"time": hn.value,
               "rx" : hn.data.rx,
               "tx" : hn.data.tx,
               "rxID" : hn.data.rxID,
               "txID" : hn.data.txID,
               "name" : hn.data.name,
               "data" : hn.data.data,
               }
    return element

def annihilate(arr, event):
    pass

def peak(arr):
    hn = lib.peak(arr)
    element = {"time": hn.value,
               "rx" : hn.data.rx,
               "tx" : hn.data.tx,
               "rxID" : hn.data.rxID,
               "txID" : hn.data.txID,
               "name" : hn.data.name,
               "data" : hn.data.data,
               }
    return element


def isEvent(arr):
    if arr.size:
        return 1
    else:
        return None

def size(arr):
    return arr.size
AttributeError:cffi库“\u heap\u i”没有名为“initQueue”的函数、常量或全局变量

但是,我在正在编译的c库中有一个名为initQueue的函数

当我在heap_inter.py上调用pypy时,执行的命令是
building'\u heap\u i'扩展
clang-DNDEBUG-O2-fPIC-I/opt/local/include-I/opt/local/lib/pypy/include-c\u heap\u I.c-o./\u heap\u I.o
cc-pthread-shared-undefined dynamic_lookup./_heap_i.o-L/opt/local/lib-o./_heap_i.pypypy-41.so
叮当声:警告:编译期间未使用参数:'-pthread'[-Wunused命令行参数]

这是我的消息来源

cheap.h

#include <stdio.h>
#include <stdlib.h>

/* priority Queue implimentation via roman10.net */
struct heapData {
  //everything from event                                         
  //tx string                                                       
  char tx[64];
  //txID int                                                        
  int txID;
  //rx string                                                       
  char rx[64];
  //rxID int                                                        
  int rxID;
  //name string                                                     
  char name[64];
  //data Object                                                     

  //time float                                                      
  float time;
};
struct heapNode {
    int value;
    struct heapData data;               //dummy                     
};
struct PQ {
    struct heapNode* heap;
    int size;
};
void insert(struct heapNode aNode, struct heapNode* heap, int size);
void shiftdown(struct heapNode* heap, int size, int idx);
struct heapNode removeMin(struct heapNode* heap, int size);
void enqueue(struct heapNode node, struct PQ *q);
struct heapNode dequeue(struct PQ *q);
struct heapNode peak(struct PQ *q);
void initQueue(struct PQ *q, int n);
int nn = 1000000;
struct PQ q;
int main(int argc, char **argv);
heap\u inter.py

from cffi import FFI

ffibuilder = FFI()

ffibuilder.set_source("_heap_i",
                      r"""//passed to C compiler                                                                                                                                                                                                                                                                                                                           
                      #include <stdio.h>                                                                                                                                                                                                                                                                                                                                   

                      #include <stdlib.h>                                                                                                                                                                                                                                                                                                                                  
                      #include "cheap.h"                                                                                                                                                                                                                                                                                                                                   
        """,
                              libraries=[])

ffibuilder.cdef("""                                                                                                                                                                                                                                                                                                                                                        
                struct heapData {                                                                                                                                                                                                                                                                                                                                          
                      char tx[64];                                                                                                                                                                                                                                                                                                                                         
                      int txID;                                                                                                                                                                                                                                                                                                                                            
                      char rx[64];                                                                                                                                                                                                                                                                                                                                         
                      int rxID;                                                                                                                                                                                                                                                                                                                                            
                      char name[64];                                                                                                                                                                                                                                                                                                                                       
                      float time;                                                                                                                                                                                                                                                                                                                                          
                      } ;                                                                                                                                                                                                                                                                                                                                                  

                      struct heapNode {                                                                                                                                                                                                                                                                                                                                    
                      int value;                                                                                                                                                                                                                                                                                                                                           
                      struct heapData data;               //dummy                                                                                                                                                                                                                                                                                                          
                      } ;                                                                                                                                                                                                                                                                                                                                                  

                      struct PQ {                                                                                                                                                                                                                                                                                                                                          
                      struct heapNode* heap;                                                                                                                                                                                                                                                                                                                               
                      int size;                                                                                                                                                                                                                                                                                                                                            
                      } ;                                                                                                                                                                                                                                                                                                                                                  
""")

#                                                                                                                                                                                                                                                                                                                                                                          
#  Rank (Simian Engine) has a Bin heap                                                                                                                                                                                                                                                                                                                                     

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)
最后测试_heap.py

import heap

pq = heap.init()

for x in range(1000) :
    heap.push({"time" : x,
               "rx" : "a",
               "tx" : "b",
               "txID" : 1,
               "rxID" : 1,
               "name" : "bob",
               "data": "none",
               },pq)

for x in range(100) :
    heap.peak(pq)

for x in range(1000):
    y = heap.dequeue(pq)
    print y

感谢任何人查看,如果您以前有过这种经历,请告诉我。

您的ffi.cdef()没有任何函数声明,因此生成的_heap_i库不会导出任何函数。尝试从.h文件复制粘贴函数声明。

您的ffi.cdef()没有任何函数声明,因此生成的_heap_i库不会导出任何函数。尝试从.h文件复制粘贴函数声明。

您必须告诉ffi.set_source()关于
heap.c
。最简单的方法是


以open('heap.c','r')作为fid:
ffi.set_源(fid.read())

比如说。它的工作方式是:

  • 可以从python访问的函数、结构、枚举和typedef的声明将出现在您的ffi.cdef(…)
  • 实现这些函数的源代码作为字符串提供给ffi.set_source(…)(您也可以在其中使用各种
    distutils
    关键字)
  • compile()构建一个共享对象,该对象导出ffi.cdef()中定义的内容,并对其进行排列,以便将这些内容公开给python

您必须告诉ffi.set\u source()关于
heap.c
。最简单的方法是


以open('heap.c','r')作为fid:
ffi.set_源(fid.read())

比如说。它的工作方式是:

  • 可以从python访问的函数、结构、枚举和typedef的声明将出现在您的ffi.cdef(…)
  • 实现这些函数的源代码作为字符串提供给ffi.set_source(…)(您也可以在其中使用各种
    distutils
    关键字)
  • compile()构建一个共享对象,该对象导出ffi.cdef()中定义的内容,并对其进行排列,以便将这些内容公开给python

hmmm。。。然后我得到这个错误
nc$pypy test\u heap.py回溯(最后一次调用):文件“test\u heap.py”,第1行,在import heap.py中,第6行,在from\u heap\u I import ffi,lib ImportError中:无法加载扩展模块“\u heap\u I.pypypy-41.so”:dlopen(\u heap\u I.pypypy-41.so,6):Symbol not found:_initqueuereference from:_heap_i.pypy-41.so中应为:_heap_i.pypypy-41.so中的平面名称空间nc$
hmmm…然后我得到此错误
nc$pypypy test_heap.py回溯(最后一次调用):文件“test_heap.py”,第1行,在导入heap.py中,第6行,在from heap\u导入ffi中,lib ImportError:无法加载扩展模块“\u heap\u i.pypy-41.so”:dlopen(\u heap\u i.pypypy-41.so,6):未找到符号:\u initQueue引用自:\u heap\u i.pypypy-41.so预期位于:heap\u i.pypypy-41.so中的平面名称空间。因此nc$
这非常有用,我认为这些文档令人困惑。推送功能可能只是最后一行,带有
元素
替换
hn
这非常有用,我认为文档很混乱。推送功能可以是最后一行,带有
元素
替换
hn