2013-10-10 28 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