且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不获取UnsatifiedLinkError的情况下加载具有依赖项的JNI .dylib文件?

更新时间:2022-05-11 01:39:17

macOS ld将库依赖项的路径构建为二进制文件.加载libSwiftHelloWorld.dylib的加载程序仅在libSwiftCode.dylib在当前目录中时才能找到它.在Java中加载依赖项无效,因为对于加载器来说,它是一个不同的库.

macOS ld builds a library dependency's path into the binary. The loader loading libSwiftHelloWorld.dylib will only find libSwiftCode.dylib if the latter is in the current directory. Loading the dependency in Java doesn't work because for the loader it's a different library.

您可以使用-install_name参数(即swiftc ... -Xlinker -install_name -Xlinker <your path>)更改libSwiftCode.dylib的内置路径.如果之后重建libSwiftHelloWorld.dylib,它将引用您给出的路径.

You can change the built-in path for libSwiftCode.dylib with the -install_name argument (i.e. swiftc ... -Xlinker -install_name -Xlinker <your path>). If you rebuild libSwiftHelloWorld.dylib afterwards it will reference the path that you gave.