错误(在Android ndk中使用C的冲突类型)

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述错误:在Android ndk中使用C的冲突类型相关的知识,希望能为你提供帮助。
我正在研究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! '); return; }

FI把lib.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! '); ^ jni/com_testing_ndk_FibLib.c:7:4: warning: passing argument 1 of 'printf' makes pointer from integer without a cast printf('Hello World! '); ^ 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' intprintf(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! '); ^ 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 hereJNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv*env, jobject thisObj) // void return type hereJNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello // jstring (=Java String JNI) here

尝试改为
public static native void sayHello();

【错误(在Android ndk中使用C的冲突类型)】并在您的头文件中:
JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello

(如果您愿意的话,还是所有jstring

    推荐阅读