I am trying to run a simple factorial program written in C++, and link it to java UI.
This is my Java code
package com.lan.factorial;
public class FacLib
{
public native static long fac(long n);
static
{
System.loadLibrary("FacLib");
}
public static long facI(long n)
{
return fac(n);
}
}
`
This is my C++ code
#include <studio.h>
#include <jni.h>
#include "com_lan_factorial_FacLib.h"
JNIEXPORT jlong JNICALL Java_com_lan_factorial_FacLib_fac(JNIEnv *env, jclass, jlong)
{
jlong f = 1;
jlong i;
for(i = 1; i <= n; i++)
{
f *= i;
}
return f;
}
this is my make file
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_lan_factorial_main.cpp
LOCAL_MODULE := com_lan_factorial_FacLib
include $(BUILD_SHARED_LIBRARY)
and I keep getting this error when I run ndk-build:
C:\Users\Lan\workspace\Factorial>"..\..\temp\android\android-ndk-r8b\ndk-build"
make: *** No rule to make target `jni/com_lan_factorial_main.cpp', needed by `obj/local/armeabi/objs/com_lan_factorial_FacLib/com_lan_factorial_main.o'. Stop.
Yes, my NDK in a different folder so I have to use ..\..\, but I do not think it is the problem. and I did run javah
As @nneonneo pointed out correctly, the name of your
.cppfile name was not correctly listed inAndroid.mk.Now you have a compilation problem in line 4:
You lost the parameter name n, but you use it in line 9:
Note that it’s OK to skip the name of the second parameter of type
jclasswhich you never use.I am not sure your compiler found the
com_lan_factorial_FacLib.hfile, but that’s not a problem: you can remove this#includestatement altogether, your code does not use it.