在Java代码中:
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)
我一直收到这个错误:
Application.mk中的来自jni / second.c:20:/home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:6:1: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 代币 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:14:1: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 代币 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 代币 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 代币 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 代币 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 代币 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:24:1: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 令牌包含在文件中 的/ home /用户名的/ dev / NDK /机器人-NDK-R9 /源/ CXX-STL / STLport的/ STLport的/载体:37:0, 来自jni / second.c:20:/home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_vector.h:752:10: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 代币 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_vector.h:760:10: 错误:在'&lt;'之前预期'=',',',';','asm'或'属性' 令牌
APP_STL:= stlport_static
答案 0 :(得分:1)
我怀疑你使用默认的C ++运行时,它不支持标准库。
请参阅ndk安装文件夹中的文档docs / CPLUSPLUS-SUPPORT.html以获取完整信息。
为了能够使用(因此包含但没有错误)vector
,您需要在Application.mk中定义APP_STL
您可以使用strlport或gnustl在本机Android开发中启用标准库,方法是添加以下内容:
APP_STL := gnustl_static
另一个问题:您尝试将vector
包含在C文件中,因此无效。