2013-10-10 78 views
-2

導入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

+0

vector是C++,你正試圖在它的C代碼? – doctorlove

+1

我強烈建議您閱讀所有的ndk示例項目,特別注意Android的makefiles。你會看到如何在那裏使用STL。 – jin

回答

1

我懷疑你使用默認的C++運行時,它不支持標準庫。

有關整個信息,請參閱ndk安裝文件夾中的文件docs/CPLUSPLUS-SUPPORT.html。

爲了能夠使用(因此,包括無誤差)vector,你需要在你Application.mk 可以使用strlport或gnustl以啓用原生Android開發的標準庫,通過添加類似定義APP_STL即:

APP_STL := gnustl_static 

另一個問題:您嘗試包括在C文件vector,所以這是行不通的。

+0

我在我的問題中進行了更新。我已經有了APP_STL的Application.mk文件:= gnustl_static – Lukap

+0

更新了我對我看到的另一個問題的回答。 – Geoffroy

+0

在jni代碼中使用矢量似乎很麻煩。所以我的問題是我如何從C++函數返回結果向量或列表 ...任何指導方針,tnx – Lukap