導入Java代碼:矢量不能JNI
System.loadLibrary("twolib-second");
int z = add(1, 2);
public native int add(int x, int y);
first.cpp:
#ifdef __cplusplus extern "C" {
#endif
using namespace std;
int first(int x, int y) {
return x*10 + y; }
#ifdef __cplusplus }
#endif
second.c:
//THIS IS THE source of trouble :) //without the include of vector works just fine //but after adding the include for vector code can't be compiled #include <vector>
#include <jni.h>
jint
Java_com_example_jniexample_MainActivity_add(JNIEnv* env,
jobject this,
jint x,
jint y)
{
return first(x, y);
}
Android.mk:
LOCAL_PATH:= $(call my-dir)
# first lib, which will be built statically
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-first
LOCAL_SRC_FILES := first.cpp
include $(BUILD_STATIC_LIBRARY)
# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-second
LOCAL_SRC_FILES := second.c
LOCAL_STATIC_LIBRARIES := libtwolib-first
include $(BUILD_SHARED_LIBRARY)
我不斷收到此錯誤:
from jni/second.c:20: /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:6:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:14:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:24:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token In file included from /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/vector:37:0, from jni/second.c:20: /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_vector.h:752:10: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_vector.h:760:10: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token
在Application.mk
APP_STL := stlport_static
vector是C++,你正試圖在它的C代碼? – doctorlove
我強烈建議您閱讀所有的ndk示例項目,特別注意Android的makefiles。你會看到如何在那裏使用STL。 – jin