setuptools和cffi:如何更改库路径

时间:2018-11-04 19:38:59

标签: python shared-libraries setuptools cffi

我正在使用cffi创建一个python程序,以针对真正的libc函数测试我的C / ASM库。

我尝试使用setuptools设置不同的cffi模块。所以这是我的文件树:

enter image description here

这是我的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)

和我的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"]
)

因此它构建良好,如您在文件树中看到的,我有我的cffi_files,但是当我尝试执行libasm_tester.py时,出现此错误:

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

我不明白为什么我会尝试从build / lib / _libasm_cffi.abi3.so打开lib,因为我告诉libasm_builder:

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

我在这里想念什么? :(。谢谢!

[编辑]

在我的libasm_tester.py中,我必须将其添加到文件顶部才能导入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

可能是原因吗?但是,如果我不添加,则无法导入cffi模块...

1 个答案:

答案 0 :(得分:0)

确定找到了解决方法:

export DYLD_LIBRARY_PATH=/path/to/your/lib

这显然很有意义,因为该库不在dlopen()使用的默认路径中...