2013-02-01 3 views
10

Ich habe eine Anwendung, die Verknüpfung mit libjvm erfordert (eine Bibliothek aus dem JDK benötigt JNI-Bindungen). Wenn ich die Position libjvm.dylib mit -L erteile, kompiliert und verbindet es erfolgreich. Allerdings, wenn ich die binäre laufen erhalte ich:Verknüpfen einer dynamischen Bibliothek (libjvm.dylib) in Mac OS X (rpath-Problem)

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

Bisher fand ich heraus, dass ich meine binäre Spezifizierungs LD_LIBRARY_PATH wie so laufen:

LD_LIBRARY_PATH=<path to libfolder installation> ./mybinary 

Aber natürlich will ich nicht. Warum sollte ich den genauen Standort trotzdem angeben, wenn ich ihn jedes Mal, wenn ich die Anwendung starte, immer wieder geben muss ?!

Ich habe auch gelernt, dass dynamische Bibliotheken auf Mac OS X bekommen eine Art von Stempel, die dort Position sagt. Allerdings weiß ich nicht, was rpath ist (scheint für mich eine Variable zu sein, aber wie kann ich es während des Linkens einstellen?).

Die Anwendung wird mit Haskell erstellt, aber ich kann die Objektdateien auch manuell mit ld verknüpfen. Ich stecke jedoch auf dieser rpath-Sache fest - ist es vielleicht etwas Besonderes für die JDK-Bibliotheken? Hier

ist das, was ich tun, um zu bauen:

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

Antwort

10

Von Apple 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. 

Sie müssen -rpath path/containing/the/library-ld passieren Wenn Sie Ihre Binärdatei verlinken, um zu erfahren, wo sie sich befindet arch, wenn das Präfix @rpath/ im Ladebefehl für die gemeinsame Bibliothek erweitert wird. Mit GHC können Sie das -optl-Wl Argument verwenden, um es Fahnen durch ld passieren zu müssen, so dass Sie GHC aufrufen wollen, wie so:

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