android - JNI中无法导入 vector

标签 android c++ c android-ndk java-native-interface

在 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/

相关文章:

android - Activity 2 中的 BroadcastReceiver 未接收到 Activity 1 中启动的服务发送的广播

c++ - 不使用系统时钟的时间测量

c++ - 计算 5 的 3 次方,但返回 0。为什么?

c++ - CUDA 内核未被所有 block 调用

c - 为什么在 printf 中使用相等时输出为 0?

c - 如何在 C 编程中重新排序数组的元素

java - ViewVideo 上的播放按钮

java - 仅替换 fragment 而不传递数据

ruby-on-rails - Android:如何在 Android 编程中编辑数据库中的特定记录(使用 Ruby on rails)

c - int num 中至少一位为 0