盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述错误:在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
)推荐阅读
- 使用OpenCV和Android NDK转换数据格式
- Android java.lang.UnsatisfiedLinkError dalvik.system.PathClassLoader
- 使用__android_log_print打印Opencv Mat,Android NDK的内容
- Android JNI - 线程同步
- Facebook Buck(BUILD FAILED:没有检测到原生平台。可能没有正确配置Android NDK)
- 如何在JavaScript中从Twig安全地打印字符串变量
- 为Symfony 1.4中的异常实现自定义500错误页面
- 使用C#和Winforms捕获不同方式的屏幕截图
- 如何在Twig中重复现有的块