python - How CFFI use the c file in a certain directory? -


i learning call c in python program cffi , write c file named 'add.c' below :

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

and python file named 'demo.py' 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 run demo.py, error add.c file cannot found. why file add.c cannot found , how can fix it?

i able reproduce error following specific error message.

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

it seems cffi trying compile file inside __pycache__ subdirectory, while add.c in current directory. fix use relative path

 #include "../add.c" 

however, once fixed that, declaration incorrect, fixed well, , following code produces correct results.

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 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -