Error: conflicting types using C in Android ndk

Error: conflicting types using C in Android ndk

我正在开发 Android 应用程序,我在其中使用 ndk 在 Java 中制作我的本机 C 代码。我的 C 和 Java 文件代码如下,我在编译时遇到类型冲突的错误。

#include <jni.h>
#include <stdio.h>
#include "com_testing_ndk_FibLib.h"

// Implementation of native method sayHello() of HelloJNI class
JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
   printf('Hello World!\n');
   return;
}

FibLib.java

package com.testing.ndk;

public class FibLib {
       static {
          System.loadLibrary("com_testing_ndk_FibLib"); // Load native library at runtime
                                       // hello.dll (Windows) or libhello.so (Unixes)
       }

       // Declare a native method sayHello() that receives nothing and returns void
       public static native String sayHello();

       // Test Driver
       public static void main(String[] args) {
          new FibLib().sayHello();  // invoke the native method
       }
    }



jni/com_testing_ndk_FibLib.c:6:24: error: conflicting types for 'Java_com_testing_ndk_FibLib_sayHello'
     JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
                            ^
    In file included from jni/com_testing_ndk_FibLib.c:3:0:
    jni/com_testing_ndk_FibLib.h:15:27: note: previous declaration of 'Java_com_testing_ndk_FibLib_sayHello' was here
     JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello
                               ^
    jni/com_testing_ndk_FibLib.c: In function 'Java_com_testing_ndk_FibLib_sayHello':
    jni/com_testing_ndk_FibLib.c:7:11: warning: character constant too long for its type
        printf('Hello World!\n');
               ^
    jni/com_testing_ndk_FibLib.c:7:4: warning: passing argument 1 of 'printf' makes pointer from integer without a cast
        printf('Hello World!\n');
        ^
    In file included from jni/com_testing_ndk_FibLib.c:2:0:
    /Users/UsmanKhan/Desktop/AndroidNDK/android-ndk-r10d/platforms/android-21/arch-arm64/usr/include/stdio.h:247:6: note: expected 'const char * __restrict__' but argument is of type 'int'
     int  printf(const char * __restrict, ...)
          ^
    jni/com_testing_ndk_FibLib.c:7:4: error: format not a string literal and no format arguments [-Werror=format-security]
        printf('Hello World!\n');
        ^
    cc1: some warnings being treated as errors
    make: *** [obj/local/arm64-v8a/objs/com_testing_ndk_FibLib/com_testing_ndk_FibLib.o] Error 1

您的方法签名不同:

public static native String sayHello(); // String return type here

JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv*env, jobject thisObj) // void return type here

JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello // jstring (=Java String JNI) here

尝试更改为

public static native void sayHello();

还有你的头文件:

JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello

(如果您愿意,也可以全部 jstring