Android - JNI 问题

Android - problems with JNI

我的 Android 项目中有以下代码(存储为 .c 文件):

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;
}

在代码中它被这样调用:

convert(r, temp1, temp2, temp3);

但是当我从命令行使用 ndk-build 命令编译它时,出现以下错误:

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);

我从 .cpp 文件中获取了该代码。当我删除签名中的地址运算符 (&) 时,一切都很好(没有错误),但我不知道算法是否会起作用。

有人可以帮忙吗?

在 C++ 中,unsigned int &c 是一个参考参数,在这种情况下,& 不是 "the address operator"。但是,C 中不存在引用,这就是为什么在 .c 文件而不是 .cpp

中编译此 C++ 代码时出现错误的原因

您正在尝试使用 C 编译器编译 C++ 代码,但这将不起作用。

显而易见的解决方案是使用 C++ 编译器编译它(例如,通过为源文件提供 .cpp 扩展名)。

如果出于某种原因必须使用 C 编译器,则必须用 C 重写函数。
对于初学者,您可以改为按值传递所有这些 const float 参数of by reference(通过去掉 &)。我不知道为什么他们首先是参考。
第一个参数 (c) 用作写入结果的 output-parameter。所以你必须将它从引用更改为指针(即 unsigned int *c)。或者更好的是,跳过输出参数并使用函数 return 代替它的结果:

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;
}

调用代码:

r = convert(temp1, temp2, temp3);