aminakoy
aminakoy

Reputation: 423

Android - problems with JNI

I have the following code (stored as .c file) in my Android project:

static void convert(unsigned int &c, const float &temp1, const float &temp2, const float &temp3) {
    if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3) * 100);

    else if ((temp3 * 2) < 1) c = (unsigned int) (temp1 * 100);

    else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.66666 - temp3) * 6) * 100);

    else c = (unsigned int) (temp2 * 100);

    return;
}

During the code it gets called like this:

convert(r, temp1, temp2, temp3);

But when I compile it with the ndk-build command from the command line, then I get the following errors:

Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-16. [arm64-v8a] Compile : com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor <= com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:193:47: error: expected ')' static void convert(unsigned int &c, const float &temp1, const float ... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:193:33: note: to match this '(' static void convert(unsigned int &c, const float &temp1, const float ... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:193:47: error: parameter name omitted static void convert(unsigned int &c, const float &temp1, const float ... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:11: error: use of undeclared identifier 'temp3' if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:27: error: use of undeclared identifier 'c' if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:57: error: use of undeclared identifier 'temp1' if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:65: error: use of undeclared identifier 'temp2' if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3) * ... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:48: error: use of undeclared identifier 'temp2' if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:194:78: error: use of undeclared identifier 'temp3' ...(temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3) * 100); ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:196:15: error: use of undeclared identifier 'temp3' else if ((temp3 * 2) < 1) c = (unsigned int) (temp1 * 100); ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:196:31: error: use of undeclared identifier 'c' else if ((temp3 * 2) < 1) c = (unsigned int) (temp1 * 100); ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:196:51: error: use of undeclared identifier 'temp1' else if ((temp3 * 2) < 1) c = (unsigned int) (temp1 * 100); ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:15: error: use of undeclared identifier 'temp3' else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.666... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:31: error: use of undeclared identifier 'c' else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.666... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:61: error: use of undeclared identifier 'temp1' else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.666... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:69: error: use of undeclared identifier 'temp2' ...if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.66666 - temp... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:52: error: use of undeclared identifier 'temp2' else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.666... ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:198:88: error: use of undeclared identifier 'temp3' ...< 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.66666 - temp3) * 6) * 100); ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:200:10: error: use of undeclared identifier 'c' else c = (unsigned int) (temp2 * 100); ^ jni/com_celik_abdullah_imageprocessingpart_processors_NativeImageProcessor.c:200:30: error: use of undeclared identifier 'temp2' else c = (unsigned int) (temp2 * 100);

I got that code from a .cpp file. When I delete the address operator (&) in the signature, then everything is fine (no errors) but I do not know if then the algorithm would work.

Can anybody help ?

Upvotes: 0

Views: 958

Answers (2)

Michael
Michael

Reputation: 58467

You're trying to compile C++ code with a C compiler, which will not work.

The obvious solution would be to compile it with a C++ compiler instead (e.g. by giving the source file a .cpp extension).

If for some reason you must use a C compiler, then you'll have to rewrite the function in C.
For starters, you can pass all those const float parameters by value instead of by reference (by getting rid of the &). I don't know why they were references in the first place.
That first parameter (c) is used as an output-parameter to which the result is written. So you'll have to change it from a reference to a pointer (i.e. unsigned int *c). Or better yet, skip the output parameter and have the function return its result instead:

static int convert(const float temp1, const float temp2, const float temp3) {
    unsigned int c;

    if ( (temp3 * 6) < 1) c = (unsigned int) ((temp2 + (temp1 - temp2) * 6 * temp3) * 100);
    else if ((temp3 * 2) < 1) c = (unsigned int) (temp1 * 100);
    else if ((temp3 * 3) < 2) c = (unsigned int) ((temp2 + (temp1 - temp2) * (.66666 - temp3) * 6) * 100);
    else c = (unsigned int) (temp2 * 100);

    return c;
}

Calling code:

r = convert(temp1, temp2, temp3);

Upvotes: 1

DeviatioN
DeviatioN

Reputation: 340

In C++ unsigned int &c is a reference parameter and & is not "the address operator" in that case. However, references don't exist in C, which is why you get errors when compiling this C++ code in a .c file instead of .cpp

Upvotes: 0

Related Questions