Search code examples
pythonpython-cffi

loading .so library with cffi


I have some C library I want to access in Python using CFFI. After building the library, I get the 2 files: $HOME/libcint/include/cint.h and $HOME/libcint/lib/libcint.so.

Now for the CFFI API mode I tried:

from cffi import FFI
libcint_dir = os.path.expanduser('~/libcint')
ffibuilder = FFI()
ffibuilder.set_source('_libcint',
  r'#include <include/cint.h>',
  include_dirs = [libcint_dir],
  libraries = ['libcint'],
  library_dirs = [os.path.join(libcint_dir, 'lib')],
)

But it fails to find the libcint.so file:

/usr/bin/ld: cannot find -llibcint

The path in libcint_dir is correct, because I don't get any error message about not finding the header file. Also I managed to successfully interface the library using the ctypes module, so the libcint.so itself should be fine.

What am I doing wrong here?

If I get this right, then there are 3 steps required here. (Please correct me, if I confused something here.)

  1. compiling libcint yielding libcint.so
  2. building the Python wrapper with CFFI
  3. importing the module built in step 2 into the actual Python program

My issue here is about the second step.


Solution

  • You're saying libraries = ['libcint'], which means that the compiler will look for a file called liblibcint.so. What you should write is thus libraries = ['cint'].