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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误:在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'
     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!
');
        ^
    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

以上是关于错误:在Android ndk中使用C的冲突类型的主要内容,如果未能解决你的问题,请参考以下文章

错误记录Android Studio 编译报错 ( VirtualApp 编译 NDK 报错 | Error:A problem occurred configuring project ‘: )(代

转NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办(代

如何定位Android NDK开发中遇到的错误

如何定位Android NDK开发中遇到的错误

Android 安装包优化动态库打包配置 ( “armeabi-v7a“, “arm64-v8a“, “x86“, “x86_64“ APK 打包 CPU 指令集配置 | NDK 完整配置参考 )(代

Android jni/ndk编程二:jni数据类型转换(primitive,String,array)