Some one help me on this please.
I am working on a simple app the does factorial on android, using ndk.
I want to have 2 .cpp files, one for class factorial, and one will be the main that invokes method from that class.
I don’t know how to deal with the header problem when it comes to build, please help.
#include "com_lan_factorial_FacLib.h"
JNIEXPORT jlong JNICALL Java_com_lan_factorial_FacLib_fac
(JNIEnv *env, jclass clazz, jlong n)
{
jlong result = (jLong) (fac(n));
return result;
}
This code call fac method. this is Main.cpp
#include <stdio>
#include "com_lan_factorial_FacLib.h"
long fac(long n)
{
long f = 1;
long i;
for(i = 1; i <= n; i++)
{
f *= i;
}
return f;
}
}
This class create fac method. This is fac.cpp
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS := -Wno-psabi
LOCAL_MODULE := libfac
LOCAL_SRC_FILES := fac.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_CFLAGS := -Wno-psabi
LOCAL_MODULE := FacLib
LOCAL_SRC_FILES := Main.cpp
LOCAL_STATIC_LIBRARIES := libfac
include $(BUILD_SHARED_LIBRARY)
The compile log is:
C:\Users\Lan\workspace\Factorial>..\..\temp\android\android-ndk-r8b\ndk-build
"Compile++ thumb : FacLib <= Main.cpp
jni/Main.cpp: In function 'jlong Java_com_lan_factorial_FacLib_fac(JNIEnv*, jclass, jlong)':
jni/Main.cpp:7:30: error: 'fac' was not declared in this scope
make: *** [obj/local/armeabi/objs/FacLib/Main.o] Error 1
Im not sure why fac is not declare
Is your second file really called
com_lan_factorial_fac.cpp.callMain? The problem with it is that does not have a.cppextension. Its extension iscallMain. Rename to something that ends with.cpp, and edit your Anrdoid.mk to list both file names:You can list all your files in one line, or you can split it up like this:
The other error message is that the file
com_lan_factorial_fac.hcan’t be found. Does such a file exist?EDIT: your Android.mk is wrong. You are not building two libraries – you’re building one, from two source files. It should go like this:
And in your Main.cpp, you should declare that a function called
fac()exists, between the #include line and theJava_com_lan_factorial_FacLib_facfunction:Your compilation error message has to do with the lack of this line.
The LOCAL_C_INCLUDES line is not generally necessary.
By the way, the builtin C datatype
longandjlongas defined by Android JNI are not the same. Better rewrite yourfac()to work withjlong‘s.