在 Java 代码中:
System.loadLibrary("twolib-second");
int z = add(1, 2);
public native int add(int x, int y);
第一个.cpp:
#ifdef __cplusplus extern "C" {
#endif
using namespace std;
int first(int x, int y) {
return x*10 + y; }
#ifdef __cplusplus }
#endif
第二个.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
最佳答案
我怀疑您使用默认的 C++ 运行时,它不支持标准库。
请参阅 ndk 安装文件夹中的文件 docs/CPLUSPLUS-SUPPORT.html 以获取完整信息。
为了能够使用(并因此包含而不会出现错误)vector
,您需要在 Application.mk 中定义 APP_STL
您可以使用 strlport 或 gnuSTL 在原生 Android 开发中启用标准库,只需添加如下内容:
APP_STL := gnustl_static
另一个问题:您尝试在 C 文件中包含 vector
,因此它不起作用。
关于android - JNI中无法导入 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19298979/