1

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)}")
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Since I can't flag due to bounty: https://stackoverflow.com/questions/3784389/difference-between-api-and-abi – shadow2020 Feb 21 '20 at 23:39
  • @shadow2020 If you think this is a good answer: What do I use in the example above and why? (I have seen the question and answers you linked, but they didn't help me) – Martin Thoma Feb 22 '20 at 05:37
  • https://cffi.readthedocs.io/en/latest/overview.html#abi-versus-api <= does this help? – Armin Rigo Apr 09 '20 at 15:37

0 Answers0