Skip to content

Python:cffi

Example

설치하기:

$ python3 -m pip install cffi

c 라이브러리 연결:

from cffi import FFI


ffibuilder = FFI()

ffibuilder.cdef("""
    double sqrt(double x);
""")

ffibuilder.set_source("_libmath",
    """
    #include <math.h>
    """,
    library_dirs = [],
    libraries = ['m']
)

ffibuilder.compile(verbose=True)

ffibuilder.compile를 마지막에 호출해야 python 링크 가능한 공유 라이브러리가 생성된다.

라이브러리 사용:

from _libmath import lib

x = lib.sqrt(4.5)

print(F"The square root of 4.5 is {x}.")

See also

Favorite site