使用Cython链接器编译c库的包装器在OSX上找不到外部c库的.dylib

2024-09-29 18:36:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用Cython为NAG(https://www.nag.co.uk/content/nag-library-c)c库中的集成函数编写了一个包装器。 它使用python setup.py build--inplace编译,其中安装文件为:

from setuptools import Extension, setup
from Cython.Build import cythonize
import re

def loadMacros(headerFile):
    """ Given a .h file, return dict of touples with macros """
    regex = re.compile("#define +(\w+) *(\w*)")
    with open(headerFile) as f:
        macros = dict(map(lambda x: re.match(regex, x).groups(),
                          [l for l in f if re.match(regex, l)]))
    # Remove recursive entries - Note this is not foolproof..
    # while not set(macros.keys()).isdisjoint(macros.values()):
    #     for k, v in macros.items():
    #         if v in macros:
    #             macros[k] = macros[v]
    return macros


nagHome = "/Users/hfmw1m17/NAG/nlmi627dbl"  # "/opt/NAG/cll6a23dhl"
macros = loadMacros(nagHome + "/lp64/include/nag.h")
macros = list(macros.items())

e = Extension("nag_integrate",
              define_macros=macros,
              sources=["nag_integrate.pyx"],
              include_dirs=[nagHome + "/lp64/include",nagHome + "/lp64/lib"],
              library_dirs=[nagHome + "/lp64/lib"],libraries=["nag_nag"],extra_objects=[
        nagHome+"/lp64/lib/libnag_nag.dylib"],runtime_library_dirs=[nagHome+"/lp64/lib/"],extra_link_args=['-Wl,-rpath']
)


setup(ext_modules=cythonize(e,annotate=True,language_level=3))enter code here

输出:

/Users/hfmw1m17/anaconda3/envs/TowingTankAcoustics/bin/python3.7 setup.py build_ext --inplace
Compiling nag_integrate.pyx because it changed.
[1/1] Cythonizing nag_integrate.pyx
running build_ext
building 'nag_integrate' extension
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/hfmw1m17/anaconda3/envs/TowingTankAcoustics/include -arch x86_64 -I/Users/hfmw1m17/anaconda3/envs/TowingTankAcoustics/include -arch x86_64 -DNAG_H= -DNAG_MICROSOFT_THREAD_SAFE= -DNAG_THREAD_SAFE= -DNULLFN=0 -DNULLDFN=0 -DNAGERR_DEFAULT= -DNAGUSER_DEFAULT= -DNAGCOMM_NULL= -DNAGMESG_DEFAULT= -DE01_DEFAULT= -DE04_DEFAULT= -DG13_DEFAULT= -DH02_DEFAULT= -DINIT_FAIL= -DSET_FAIL= -DINIT_MESG= -DINIT_STREAM= -DRDUMMY= -DIDUMMY= -DINIT2DUMMY= -DVprintf= -DVfprintf= -DVsprintf= -DVscanf= -DVfscanf= -DVstrcpy= -DABS= -DFABS= -DSIGN= -DMAX= -DMIN= -DDROUND= -DROUND= -DSQZABS= -DCONJ= -DVCONJ= -DZMULT= -D_nag_expand= -Dnag_stringize= -I/Users/hfmw1m17/NAG/nlmi627dbl/lp64/include -I/Users/hfmw1m17/NAG/nlmi627dbl/lp64/lib -I/Users/hfmw1m17/anaconda3/envs/TowingTankAcoustics/include/python3.7m -c nag_integrate.c -o build/temp.macosx-10.9-x86_64-3.7/nag_integrate.o
gcc -bundle -undefined dynamic_lookup -L/Users/hfmw1m17/anaconda3/envs/TowingTankAcoustics/lib -arch x86_64 -L/Users/hfmw1m17/anaconda3/envs/TowingTankAcoustics/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.9-x86_64-3.7/nag_integrate.o /Users/hfmw1m17/NAG/nlmi627dbl/lp64/lib/libnag_nag.dylib -L/Users/hfmw1m17/NAG/nlmi627dbl/lp64/lib -L/Users/hfmw1m17/NAG/nlmi627dbl/lp64/lib/ -lnag_nag -o build/lib.macosx-10.9-x86_64-3.7/nag_integrate.cpython-37m-darwin.so -Wl,-rpath
copying build/lib.macosx-10.9-x86_64-3.7/nag_integrate.cpython-37m-darwin.so -> 

Process finished with exit code 0

但是,当我从创建的.so对象导入函数时,出现以下错误:

ImportError: dlopen(/Users/hfmw1m17/WaterTankISM/WaterTankISM/nag_integration/nag_integrate.cpython-37m-darwin.so, 2): Library not loaded: libnag_nag.dylib
  Referenced from: /Users/hfmw1m17/WaterTankISM/WaterTankISM/nag_integration/nag_integrate.cpython-37m-darwin.so
  Reason: image not found

libnag_nag.dylib是nag生成的动态库

在我的包装器的共享对象上使用otool-L会导致:

libnag_nag.dylib (compatibility version 0.0.0, current version 27.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)

我认为这是链接器在编译时无法找到动态库的问题。对如何解决这个问题有什么建议吗

非常感谢


Tags: builddefaultincludelibusersx86macrosintegrate
1条回答
网友
1楼 · 发布于 2024-09-29 18:36:37

使用以下方法解决:

install_name_tool -add_rpath path_to_dylib_directory

在运行python setup.py build in place之后

如果任何人都能理解的话,我想在setup.py文件中找到这样做的方法

非常感谢

相关问题 更多 >

    热门问题