Search code examples
pythonshared-librariessetuptoolscffi

setuptools and cffi: how to change the library path


i am creating a python program with cffi to test my C/ASM library against the real libc functions.

i try to use setuptools to setup my different cffi modules. so here is my filetree:

enter image description here

here is my libftasm_builder.py:

from cffi import FFI

ffiBuilder = FFI()

ffiBuilder.cdef("""
    void    ft_bzero(void *s, size_t n);
    char    *ft_strcat(char *dest, const char *src);
    int     ft_isalpha(int c);
    int     ft_isdigit(int c);
    int     ft_isalnum(int c);
    int     ft_isascii(int c);
    int     ft_isprint(int c);
    int     ft_toupper(int c);
    int     ft_tolower(int c);
    int     ft_puts(const char *s);
    size_t  ft_strlen(const char *s);
    void    *ft_memset(void *s, int c, size_t n);
    void    *ft_memcpy(void *dest, const void *src, size_t n);
    char    *ft_strdup(const char *s);
    void    ft_cat(int fd);
    int     ft_islower(int c);
    int     ft_isupper(int c);
""")

ffiBuilder.set_source("_libasm_cffi",
    """
        #include "libftasm.h"
    """,
    include_dirs=['/Users/sle-lieg/libasm/libftasm/header/'],
    library_dirs=['/Users/sle-lieg/libasm/libftasm/'],
    libraries=['ftasm']
)

if __name__ == "__main__":
    ffiBuilder.compile(verbose=True)

and my setup.py:

from setuptools import setup

setup(
    setup_requires=["cffi>=1.0.0"],
    cffi_modules=[
        "libftasm_builder.py:ffiBuilder",
        "libc_builder.py:ffiBuilder"
    ],
    install_requires=["cffi>=1.0.0"]
)

So it builds fine, as you can see in the filetree i have my cffi_files, but when i try to execute my libasm_tester.py, i have this error:

Traceback (most recent call last):
  File "libasm_tester.py", line 4, in <module>
    from _libasm_cffi import ffi, lib
ImportError: dlopen(/Users/sle-lieg/libasm/libftasm/lib_tester/build/lib.macosx-10.12-x86_64-3.7/_libasm_cffi.abi3.so, 2): Library not loaded: libftasm.dylib
  Referenced from: /Users/sle-lieg/libasm/libftasm/lib_tester/build/lib.macosx-10.12-x86_64-3.7/_libasm_cffi.abi3.so
  Reason: image not found

i don't understand why it would try to open the lib from build/lib/_libasm_cffi.abi3.so since i tell to libasm_builder:

library_dirs=['/Users/sle-lieg/libasm/libftasm/']

what am i missing here ? :( . thank you !!

[EDIT]

in my libasm_tester.py, i had to add that on top of my file to be able to import the cffi_modules:

import sys
sys.path.insert(0, '/Users/sle-lieg/libasm/libftasm/lib_tester/build/lib.macosx-10.12-x86_64-3.7/')

from _libasm_cffi import ffi, lib
from _libc_cffi import lib as libC

maybe the reason ? but if i don't add that, i can't import the cffi modules ...


Solution

  • ok found a solution:

    export DYLD_LIBRARY_PATH=/path/to/your/lib
    

    which make sense obviously since the library is not in default path used by dlopen() ...