The first time I met this block type it wasn't for "java" but "C", so I think it's the same interest. The block defined by:
extern "Java"
{
// some java definition
}
is used to specifie to GCC that this block is a java interface. It's use for describe which type of mangling used to define the class. The name mangling is use by gcc to generate function name by parameters, etc... More informations here : http://www.agner.org/optimize/calling_conventions.pdf
So you use an extern "Java"
when you importe java code with it you can call it juste like any function in C/C++ without specifie the mangled name,. My only use of it was for a dll with some C functions defined into, loaded in a C++ code, so I use an extern "C" to specifie to GCC that the definition of this function don't use name mangling.
Well, now how to call a native method in java because all in java is method, all is object, there is no function. First, you have to describe you class in java, all of your functions that you want to do in native langage have to be defined as native : private native void print();
for exemple. Secondly, back in your native code header, you must define the method following the nomenclature :
extern "Java"
{
JNIEXPORT YourReturnType JNICALL Java_ClassName_MethodName (JNIEnv* env, jobject obj);
}
At least, all method must look like that, cause JNI will send a JNIEnv pointer and a object that will be "this" in the method, if you have other arguments, they must be gave after the 2 basics. Finally, you just have to implement all method in a native code file, always following the norme, like :
JNIEXPORT void JNICALL Jave_Printer_print(JNIEnv* env, jobject obj)
{
printf("Hello world");
}
Now you can create a Printer object in Java and call print method defined as native.
I hope I answered your question.
gcj
? – Absa