【发布时间】:2020-06-22 07:15:37
【问题描述】:
我有一个带有外部函数的简单 cpp 文件。一个函数返回类 Abc 的引用,另一个接收它并调用某个方法:
extern "C" {
void* getMyObject(int arg1, int arg2)
return new Abc();
}
void testMyObject(Abc* p) {
p->test()
}
还有一个调用外部函数的 Java 代码:
@Platform(include="export.cpp", link="mylib")
class MyIterface {
static {
Loader.load()
}
public static native Pointer getMyObject(int a, int b);
public static native void testMyObject(Pointer op);
}
我这样称呼他们:
MyInterface lib = new MyInterface();
Pointer op = lib.getMyObject(111, 222); //get the reference to Abc object
lib.testMyObject(op); //Pass the Abc pointer to invoke its method
但我收到以下错误:"
/home/Project/target/classes/com/proj/dl4j/jniMyInterface.cpp: In function ‘void Java_com_proj_dl4j_MyInterface_testMyPointer(JNIEnv*, jclass, jobject)’:
/home/Project/target/classes/com/proj/dl4j/jniMyInterface.cpp:629:27: error: cannot convert ‘char*’ to ‘Abc*’ for argument ‘1’ to ‘void testMyPointer(Abc*)’
testMyPointer(ptr0);
^
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.464 s
[INFO] Finished at: 2020-06-22T12:22:47+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.bytedeco:javacpp:1.5.3:build (process-classes) on project MyProject: Execution process-classes of goal org.bytedeco:javacpp:1.5.3:build failed: Process exited with an error: 1 -> [Help 1]
我在void* 上进行了投射,即:Abc* ob = (Abc*)p 有效。但我仅限于修改这些外部函数(设计问题!!)。有什么解决办法吗??
【问题讨论】:
标签: java c++ java-native-interface javacpp jniwrapper