需要帮助链接到OS X上的打包 [英] Need help linking to bundle on OS X

查看:200
本文介绍了需要帮助链接到OS X上的打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个有经验的Java编码器,但我是XCode和C ++的新手,所以很抱歉的愚蠢的问题。



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

  //获取与Java VM关联的JNIEnv *,必要时创建JVM 
//实例。注意,这个例程的实现
//必须准备好从多个线程调用它。
JNIEnv * JavaVM_GetJNIEnv();

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

  -force_load / System / Library / Java / Deploy.bundle / Contents / Resources / JavaPlugin2_NPAPI.plugin / Contents / MacOS / JavaPlugin2_NPAPI 

一个符号链接;实际路径是/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,不能与bundle绑定(MH_BUNDLE)dylibs(MH_DYLIB)
collect2:ld返回1退出状态

解决方案

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

  NPError(* getEntryPoints)(NPPl​​uginFuncs * aNPPFuncs); //定义一个指向函数的指针的变量

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(*)(NPPl​​uginFuncs *))CFBundleGetFunctionPointerForName(bundleRef,CFSTR(NP_GetEntryPoints)); //将指针函数设置为从bundle中加载的函数

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

NPError err = getEntryPoints(& pluginFuncs); //这是实际调用库函数
// ...使用插件API ...更多的东西...
}

另一方面,这并没有对我的目的非常有用,因为据我所知,java插件API只是被设计为从基于Mozilla的浏览器,我试图嵌入Java在我自己的应用程序。


I'm an experienced Java coder, but I'm new to XCode and C++, so sorry for the dumb question.

I'm writing some c++ code in XCode that needs to instantiate a Java Virtual Machine. There is a method in the OS X Java plugin called JavaVM_GetJNIEnv(), and a header file in the source code from Sun/Oracle called JavaVM.h with these lines:

// 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();

I added the .h file to my XCode project, but I don't know how to link to the binary file. I figured out how to force-load in the linker, like this:

-force_load /System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin/Contents/MacOS/JavaPlugin2_NPAPI

(this file is a symbolic link; the real path is /System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin/Contents/Resources/Java/libplugin2_npapi.jnilib)

But then I get this error message:

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

So my question is, how do I link to code in a .jnilib file with XCode?

解决方案

I figured it out. If you're trying to reference code stored in a .bundle, you don't actually link to it, you make calls to it at runtime and then reference the functions by name (ie. similar to Java's reflection, which I'm more familiar with).

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 ...
}

As an aside, this didn't wind up being very useful for my purposes because as far as I can tell, the java plugin API is only designed to be called from Mozilla-based browsers, and I'm trying to embed Java in my own application.

这篇关于需要帮助链接到OS X上的打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆