Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从JNI方法调用纯C函数_Android_Java Native Interface - Fatal编程技术网

Android 从JNI方法调用纯C函数

Android 从JNI方法调用纯C函数,android,java-native-interface,Android,Java Native Interface,我正在研究JNi示例。我有一个C文件: #include "test_lib.h" #include <mobile_crypto.h> #include <stdio.h> int mainCrypto(void){ . . . . return 1 ; } #包括“test_lib.h” #包括 #包括 int mainCrypto(无效){ . . . . 返回1; } 现在,我想从另一个写JNI方法的C文件访问mainCrypto()方法: #include

我正在研究JNi示例。我有一个C文件:

#include "test_lib.h"
#include <mobile_crypto.h>
#include <stdio.h>
int mainCrypto(void){
.
.
.
.
return 1 ;
}
#包括“test_lib.h”
#包括
#包括
int mainCrypto(无效){
.
.
.
.
返回1;
}
现在,我想从另一个写JNI方法的C文件访问mainCrypto()方法:

#include "com_My_NativeLib.h"
#include "test_lib.h"
#include <stdio.h>
#include <jni.h>
JNIEXPORT jint JNICALL Java_com_My_NativeLib_crypt(JNIEnv * env, jobject obj){


    int status = 0; 
    status = mainCrypto();

    return status;
}
#包括“com_My_NativeLib.h”
#包括“test_lib.h”
#包括
#包括
JNIEXPORT jint JNICALL Java_com_My_NativeLib_crypt(JNIEnv*env,jobject obj){
int status=0;
status=maincypto();
返回状态;
}
我想知道是否可以像本例中那样从另一个C文件调用纯C函数mainCrypto()。我没有得到任何返回值,因此状态变量值没有得到更新

提前谢谢

我遇到的一些错误: 调试/NDK_NativeLib(3640):[09-12 15:58:40.766 3640:0xe38 F//system/bin/app_进程] 调试/NDK_NativeLib(3640):检测到堆栈损坏:中止 调试/合子(33):进程3640被信号(6)终止
INFO/ActivityManager(41):进程com.crypto(pid 3640)已死亡。

为什么您认为您可能无法从其他C函数调用
maincypto()
?很明显,你没有理由不能,但是我认为你不会问这个问题,除非你有理由认为你不能。除非您遗漏了一些奇怪的细节,是的,您也可以从其他地方调用它。

创建h文件,例如带有follow text的maincypto.h

int mainCrypto(无效)


在您的带有Java_com_My_NativeLib_crypt的cpp文件中添加“main crypto.h”,仅此而已。

@Henry Pushel:谢谢您的回复。我已经按照您的建议创建了头文件。未执行mainCrypto()中的返回语句。我的错误日志是DEBUG/NDK_NativeLib(3640):[09-12 15:58:40.766 3640:0xe38 F//system/bin/app_process]DEBUG/NDK_NativeLib(3640):检测到堆栈损坏:中止调试/合子(33):进程3640被信号(6)INFO/ActivityManager(41)终止。进程com.crypto(pid 3640)已死亡。看起来您的代码中有错误。通过H文件定义函数不能成为此错误的原因。尝试注释mainCrypto中的所有代码