需要帮助链接到OS X上的bundle

Jes*_*num 5 macos xcode linker cocoa bundle

我是一名经验丰富的Java程序员,但我是XCode和C++的新手,所以对于这个愚蠢的问题感到抱歉.

我正在XCode中编写一些需要实例化Java虚拟机的c ++代码.OS X Java插件中有一个名为JavaVM_GetJNIEnv()的方法,以及来自Sun/Oracle的源代码中的头文件,名为JavaVM.h,其中包含以下行:

// Gets the JNIEnv* associated with the Java VM, creating the JVM
// instance if necessary. Note that the implementation of this routine
// must be prepared for it to be called from more than one thread.
JNIEnv* JavaVM_GetJNIEnv();
Run Code Online (Sandbox Code Playgroud)

我将.h文件添加到我的XCode项目中,但我不知道如何链接到二进制文件.我想出了如何在链接器中强制加载,如下所示:

-force_load /System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin/Contents/MacOS/JavaPlugin2_NPAPI
Run Code Online (Sandbox Code Playgroud)

(此文件是符号链接;真实路径为/System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin/Contents/Resources/Java/libplugin2_npapi.jnilib)

但后来我收到此错误消息:

ld: in /System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin/Contents/MacOS/JavaPlugin2_NPAPI, can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何使用XCode链接到.jnilib文件中的代码?

Jes*_*num 2

我想到了。如果你试图引用存储在 .bundle 中的代码,你实际上并没有链接到它,而是在运行时调用它,然后按名称引用函数(即类似于 Java 的反射,我更喜欢它)熟悉)。

NPError (*getEntryPoints)(NPPluginFuncs *aNPPFuncs); //Defines a variable which is a pointer to a function

CFURLRef bundleUrl = CFURLCreateWithFileSystemPath(NULL, CFSTR("/System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin"), kCFURLPOSIXPathStyle, true);
CFBundleRef bundleRef = CFBundleCreate(NULL, bundleUrl);
getEntryPoints = (NPError (*)(NPPluginFuncs *))CFBundleGetFunctionPointerForName ( bundleRef, CFSTR("NP_GetEntryPoints" ) ); //Sets the pointer function to a function loaded from the bundle

if( getEntryPoints == NULL ) {
    printf("getEntryPoints is NULL");
} else {
    NPPluginFuncs pluginFuncs;
    pluginFuncs.size = sizeof(NPPluginFuncs);

    NPError err = getEntryPoints( &pluginFuncs ); //This is what actually calls the library function
    //... do more stuff with plugin API ...
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,这对我的目的来说并不是很有用,因为据我所知,java 插件 API 只能从基于 Mozilla 的浏览器调用,而且我正在尝试将 Java 嵌入到我的应用程序中。自己的应用程序。