使用完整路径链接到 Mac 上的动态库

发布于 2024-08-15 11:38:30 字数 1456 浏览 6 评论 0原文

我正在使用以下命令(使用 cmake 生成)链接嵌入 Matlab 引擎的(Python 扩展)库,

c++ -mmacosx-version-min=10.6 -bundle -headerpad_max_install_names  -o library.so library.o /Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib /Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib -framework Python

导致

$ otool -L library.so
library.so:
    @loader_path/libeng.dylib (compatibility version 0.0.0, current version 0.0.0)
    @loader_path/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.1)
    /opt/local/lib/gcc44/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.13.0)
    /opt/local/lib/gcc44/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.0)

然而,当我尝试使用该库时,我收到一条错误消息:

ImportError: dlopen(./library.so, 2): Library not loaded: @loader_path/libmex.dylib
  Referenced from: ./library.so
  Reason: image not found

我相信问题源于以下事实:链接器包含 @loader_path/libeng.dylib 形式的 matlab dylib 文件,而不是使用完整路径,即使我提供了 g++ 的完整路径。如何强制链接器使用完整路径?

我知道一种解决方案是使用

export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2009b.app/bin/maci64:$DYLD_LIBRARY_PATH

这些库文件所在的位置,但我想避免这种情况,因为它会导致一些其他问题。

I am linking a (Python extension) library that embeds the Matlab engine with the following command (generated using cmake)

c++ -mmacosx-version-min=10.6 -bundle -headerpad_max_install_names  -o library.so library.o /Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib /Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib -framework Python

resulting in

$ otool -L library.so
library.so:
    @loader_path/libeng.dylib (compatibility version 0.0.0, current version 0.0.0)
    @loader_path/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.1)
    /opt/local/lib/gcc44/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.13.0)
    /opt/local/lib/gcc44/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.0)

However, when I try to use the library, I get an error message:

ImportError: dlopen(./library.so, 2): Library not loaded: @loader_path/libmex.dylib
  Referenced from: ./library.so
  Reason: image not found

I believe the problem stems from the fact that the linker includes the matlab dylib files in the form @loader_path/libeng.dylib rather than using the full path, even though I give the full path to g++. How can I force the linker to use the full path?

I know one solution is to use

export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2009b.app/bin/maci64:$DYLD_LIBRARY_PATH

which is where those library files reside, but I'd like to avoid that as it causes some other problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜声 2024-08-22 11:38:30

使用 install_name_tool 手动更改文件

install_name_tool -change "@loader_path/libeng.dylib" "/Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib" library.so 
install_name_tool -change "@loader_path/libmx.dylib" "/Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib" library.so 

我可以使用它作为临时修复,但我想知道是否没有更好的解决方案,为链接器提供使用完整路径的设置。

Manually changing the files using install_name_tool

install_name_tool -change "@loader_path/libeng.dylib" "/Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib" library.so 
install_name_tool -change "@loader_path/libmx.dylib" "/Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib" library.so 

I could use this as a temporary fix, but I wonder if there isn't a better solution where the linker is given a setting to use the full paths.

ヤ经典坏疍 2024-08-22 11:38:30

请注意,可以通过使用 DYLD_FALLBACK_LIBRARY_PATH 来避免 DYLD_LIBRARY_PATH 的一些问题。仅当在默认路径中找不到该库时才会使用此选项。

Note that some of the problems with DYLD_LIBRARY_PATH can be prevented by using DYLD_FALLBACK_LIBRARY_PATH instead. This will only be used if the lib cannot be found in the default paths.

纸短情长 2024-08-22 11:38:30

查看 ld 命令的 -rpath 选项来控制它。您可能还对 https://github.com/bimargulies/jni-origin 的内容感兴趣-testbed,这是一些相关技术的演示。

这里的关键技术是:

install_name_tool -change libsl2.so "@loader_path/libsl2.so" libsl1.so

Look into the -rpath option to the ld command to control this. You might also be interested in the contents of https://github.com/bimargulies/jni-origin-testbed, which is a demonstration of some relevant technology.

The critical technique here is:

install_name_tool -change libsl2.so "@loader_path/libsl2.so" libsl1.so
心作怪 2024-08-22 11:38:30

您还可以使用符号链接!

You can also use symbolic link !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文