DaringLi
DaringLi

Reputation: 409

What is the difference between jni compile cpp and c?

test.c

#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>

jstring Java_com_test_b_hello_hellostr( JNIEnv* env,jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}

This compilation is OK. But compile has the error when I change to test.cpp.

libb/jtest.cpp: In function '_jstring* Java_com_test_b_hello_hellostr(JNIEnv*, _jobject*)': jtest.cpp:108: error: base operand of '->' has non-pointer type '_JNIEnv' make[1]: * [out/.../obj/SHARED_LIBRARIES/libdrvb_intermediates/jtest.o] Error 1

Why it like this? It has the difference between app and c?

I check the system jni.h file: alps\dalvik\libnativehelper\include\nativehelper\jni.h .

.
    void        (*ReleaseStringChars)(JNIEnv*, jstring, const jchar*);
    jstring     (*NewStringUTF)(JNIEnv*, const char*);
    jsize       (*GetStringUTFLength)(JNIEnv*, jstring);
...
    jstring NewStringUTF(const char* bytes)
    { return functions->NewStringUTF(this, bytes); }
.....

Upvotes: 1

Views: 601

Answers (1)

steveha
steveha

Reputation: 76735

JNI for C++ is slightly different from JNI for plain C.

In plain C it is correct to use: (*env)->SomeFunction(env, arg, arg, ...)

Note that you must dereference env and that the first argument to the function is always env.

In C++, it's different. You use: env->SomeFunction(arg, arg, ...)

You don't need to dereference env and you don't pass env as the first argument.

The actual calls into Java will be the same. Java doesn't care whether you use plain C or C++ to do the JNI stuff.

Here's a quick introduction to using C++ for JNI.

http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/cpp.html

Upvotes: 2

Related Questions