gpt4 book ai didi

macos - 在 Mac OS X 中链接动态库 (libjvm.dylib)(rpath 问题)

转载 作者:行者123 更新时间:2023-12-02 04:36:54 26 4
gpt4 key购买 nike

我确实有一个需要与 libjvm 链接的应用程序(JDK 中需要进行 JNI 绑定(bind)的库)。当我使用 -L 告诉 libjvm.dylib 的位置时,它成功编译和链接。但是,当我运行二进制文件时,我得到:

dyld: Library not loaded: @rpath/libjvm.dylib
Referenced from: <my home directory>/./mybinary
Reason: image not found

到目前为止,我发现我可以运行指定 LD_LIBRARY_PATH 的二进制文件,如下所示:

LD_LIBRARY_PATH=<path to libfolder installation> ./mybinary

但我当然不希望这样。如果每次启动应用程序时都必须一次又一次地指定确切位置,为什么还要指定确切位置?!

我还了解到,mac os x 上的动态库确实会获得一种标记,表明其位置。但是我不知道 rpath 是什么(对我来说似乎是一个变量,但如何在链接过程中设置它?)。

该应用程序是使用 haskell 构建的,但我同样可以使用 ld 手动链接对象文件。然而,我陷入了 rpath 的困境 - 它对于 JDK 库来说可能是特殊的吗?

这是我为了构建而所做的事情:

ghc --make Main.hs mycbinding.o -ljvm -L<javahome>/jre/lib/server -o mybinary

最佳答案

来自苹果的dyld man page :

@rpath/

  Dyld maintains a current stack of paths called the run path list.
When @rpath is encountered it is substituted with each path in the
run path list until a loadable dylib if found. The run path stack
is built from the LC_RPATH load commands in the depencency chain
that lead to the current dylib load. You can add an LC_RPATH load
command to an image with the -rpath option to ld(1). You can even add
a LC_RPATH load command path that starts with @loader_path/, and it
will push a path on the run path stack that relative to the image
containing the LC_RPATH. The use of @rpath is most useful when you
have a complex directory structure of programs and dylibs which can be
installed anywhere, but keep their relative positions. This scenario
could be implemented using @loader_path, but every client of a dylib
could need a different load path because its relative position in the
file system is different. The use of @rpath introduces a level of
indirection that simplies things. You pick a location in your directory
structure as an anchor point. Each dylib then gets an install path that
starts with @rpath and is the path to the dylib relative to the anchor
point. Each main executable is linked with -rpath @loader_path/zzz,
where zzz is the path from the executable to the anchor point. At runtime
dyld sets it run path to be the anchor point, then each dylib is found
relative to the anchor point.

在链接二进制文件时,您需要将 -rpath path/containing/the/library 传递给 ld,以告诉它在扩展 @rpath 时在何处搜索/ 共享库加载命令中的前缀。使用 GHC,您可以使用 -optl-Wl 参数将标志传递给 ld,因此您需要像这样调用 GHC:

ghc --make Main.hs mycbinding.o -ljvm -L<javahome>/jre/lib/server -optl-Wl,-rpath,<javahome>/jre/lib/server -o mybinary

关于macos - 在 Mac OS X 中链接动态库 (libjvm.dylib)(rpath 问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14656657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com