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

 2023-01-19    375  

问题描述

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

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

到目前为止,我发现我可以运行我的二进制指定ld_library_path这样的二进制文件:

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

LD_LIBRARY_PATH=<path to libfolder installation> ./mybinary

但是,我当然不想要那个.如果我每次启动应用程序时必须一次又一次地给予确切的位置,为什么要指定确切的位置?!

我还了解到,Mac OS X上的动态库确实得到了一种邮票,该邮票告诉那里位置.但是我不知道rpath是什么(对我来说似乎是一个变量,但是在链接时我该如何设置?).

该应用程序是使用Haskell构建的,但是我可以很好地使用ld手动链接对象文件.但是,我陷入了那件事 – 可能对JDK库特别吗?

这是我为构建而要做的:

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

推荐答案

来自苹果的https://develveper.apple.com/legacy/library/documentation/darwin/darwin/reference/manpages/man1/man1/man1/dyld.1.html” 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

以上所述是小编给大家介绍的在Mac OS X中链接动态库(libjvm.dylib)(rpath问题)。,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/25782.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。