0

我有一个带有外部函数的简单 cpp 文件。一个函数返回类 Abc 的引用,另一个接收它并调用一些方法:

extern "C" {

void* getMyObject(int arg1, int arg2)
     return new Abc();
}

void testMyObject(Abc* p) {
     p->test()
}

以及调用 extern 函数的 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哪个有效。但我仅限于修改这些外部函数(设计问题!!)。有什么解决方法吗??

4

1 回答 1

1

修改方法public static native void testMyObject(@Cast("Abc *") Pointer op);工作!

于 2020-06-22T07:51:05.400 回答