Home:ALL Converter>What is the difference between cffi out-of-line API and ABI mode?

What is the difference between cffi out-of-line API and ABI mode?

Ask Time:2020-02-19T03:43:32         Author:Martin Thoma

Json Formatter

I'm currently learning about interfacing Python and C code (e.g. in this case with cffi). I understood that "out-of-line mode" means that the C-code was compiled to a shared object at install time and in-line mode means that it is done at import time.

I struggle with how to recognize ABI vs API mode in cffi. Is the following MVCE an example for ABI or API?

MVCE

libfib.cpp

int fib(int n) {
    int a = 0, b = 1, i, tmp;
    if (n <= 1) {
        return n;
    }

    for (int i = 0; i < n - 1; i++) {
        tmp = a + b;
        a = b;
        b = tmp;
    }

    return b;
}

extern "C" {
    extern int cffi_fib(int n) {
        return fib(n);
    }
}

Compile with g++ -o ./libfib.so ./libfib.cpp -fPIC -shared

import cffi

ffi = cffi.FFI()
ffi.cdef("int cffi_fib(int n);")
C = ffi.dlopen("./libfib.so")

for i in range(10):
    print(f"{i}: {C.cffi_fib(i)}")

Author:Martin Thoma,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60288337/what-is-the-difference-between-cffi-out-of-line-api-and-abi-mode
yy