简体   繁体   English

C ++ JNI互操作性

[英]C++ JNI interoperability

After a thorough search of these forums, and being unable to find a relevant answer; 在对这些论坛进行彻底搜索之后,无法找到相关答案; I decided to ask you, the kind people of StackOverflow my question. 我决定问您,StackOverflow的好心人。

I currently have 3 (C++) libraries: 我目前有3个(C ++)库:

  • StaticLib (containing pure virtual interfaces and "C" style prototypes) StaticLib(包含纯虚拟接口和“ C”样式的原型)

  • DynamicLibLinux (containing Linux implementation of above prototypes) DynamicLibLinux(包含上述原型的Linux实现)

  • DynamicLibAndroid (not sure what to put in here) DynamicLibAndroid(不确定在此处放入什么)

The problem is, I don't want to change the StaticLib's code which is: 问题是,我不想更改StaticLib的代码,即:

struct IObject {
 virtual ~IObject() {}
 virtual void foo() = 0;
};

extern "C" { 
 IObject* CreateObject();
}

The DynamicLibLinux contains DynamicLibLinux包含

#include <IObject.h>
class Object : public IObject {
public:
 virtual ~Object() {}
 virtual void foo() {
  //do something incredibly useful here...
 }
};

#ifdef __cplusplus
extern "C" {
#endif
IObject* CreateObject() {
 return new Object;
}
#ifdef __cplusplus
}
#endif

after which, in my linux standalone, I can call the dlsym("libDynamicLibLinux.so") function to load my CreateObject function. 之后,在独立的linux中,我可以调用dlsym(“ libDynamicLibLinux.so”)函数来加载我的CreateObject函数。 And it all works like a charm. 这一切都像一个魅力。 Now, here's the thing: I want to use the static library in an Android application as well. 现在,事情来了:我也想在Android应用程序中使用静态库。 And I can't figure out how to implement this in JNI. 而且我不知道如何在JNI中实现这一点。 So far I have something like this: 到目前为止,我有这样的事情:

#include <IObject.h>
#include <jni.h>
class Object : public IObject {
... same as above
};

#ifdef ...
JNIEXPORT IObject* JNICALL CreateObject(JNIEnv*) {
 return new Object;
}

which will, obviously, not work since the implementation doesn't match the declaration. 显然,这将不起作用,因为实现与声明不匹配。 So my question boils down to: 所以我的问题可以归结为:

Is there any way I can use the C++ static lib "as-is" with JNI? 有什么办法可以将J ++ C ++静态库按原样使用?

Kind regards and thank you for you patience, 亲切的问候,感谢您的耐心配合,

Emiel 艾米尔

EDIT: In the meantime, i've found something promising: javacpp. 编辑:在此期间,我发现了一些有希望的东西:javacpp。 Does anyone here have experience with using this library? 这里有人有使用该库的经验吗?

I'm not sure I understand how your system works, but yes something like this interface should work with JavaCPP : 我不确定我是否了解您的系统如何工作,但是是的,像这样的接口应该可以与JavaCPP一起工作:

@Platform(include="IObject.h",link="DynamicLib")
public class DynamicLib {
    public static class IObject extends Pointer {
        public native void foo();
    }
    public static native IObject CreateObject();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM