0

I am learning to call c in python program by CFFI and write c file named 'add.c' as below :

float add(float f1, float f2)
{
    return f1 + f2;
}

and a python file named 'demo.py' to call add method in 'add.c':

from cffi import FFI

ffi = FFI()
ffi.cdef("""
   float(float, float);
""")

C = ffi.verify("""
   #include 'add.c'
 """, libraries=[]
)

sum = C.add(1.9, 2.3)
print sum

When I run demo.py, I get the error that add.c file cannot be found. Why file add.c cannot be found and how can I to fix it?

4

1 回答 1

3

我能够通过以下特定错误消息重现您的错误。

__pycache__/_cffi__x46e30051x63be181b.c:157:20: fatal error: add.c: No such file or 
directory
    #include "add.c"

似乎cffi正在尝试从__pycache__子目录中编译您的文件,而add.c在当前目录中。解决方法是使用相对路径

 #include "../add.c"

但是,一旦我修复了它,您的声明也是不正确的,所以我也修复了它,下面的代码产生了正确的结果。

from cffi import FFI

ffi = FFI()
ffi.cdef("""
   float add(float f1, float f2);
""")

C = ffi.verify("""
   #include "../add.c"
 """, libraries=[]
)

sum = C.add(1.9, 2.3)
print sum
于 2014-04-10T06:45:24.937 回答