【问题标题】:Vector can not be imported in JNI在 JNI 中无法导入矢量
【发布时间】:2013-10-10 14:45:03
【问题描述】:

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

我不断收到此错误:

来自 jni/second.c:20: /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:6:1: 错误:在 'attribute' 令牌 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:14:1: 错误:在 'attribute' 令牌 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在 'attribute' 令牌 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在 'attribute' 令牌 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在 'attribute' 令牌 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在 'attribute' 令牌 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:24:1: 错误:在 'attribute' 令牌包含在文件中 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/vector:37:0, 来自 jni/second.c:20: /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_vector.h:752:10: 错误:在 'attribute' 令牌 /home/用户名/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_vector.h:760:10: 错误:在 'attribute' 令牌

在 Application.mk 中

APP_STL := stlport_static

【问题讨论】:

  • vector 是 C++ 并且您正在尝试在其中使用 C 代码?
  • 我强烈建议您阅读所有 Ndk 示例项目,特别注意 Android makefile。您将在那里看到如何使用 STL。

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


【解决方案1】:

我怀疑你使用了不支持标准库的默认 C++ 运行时。

请参阅 ndk 安装文件夹中的 docs/CPLUSPLUS-SUPPORT.html 文件以获取全部信息。

为了能够使用(因此,正确地包含)vector,您需要在 Application.mk 中定义 APP_STL 您可以使用 strlport 或 gnustl 在原生 Android 开发中启用标准库,方法是添加如下内容:

APP_STL := gnustl_static

另一个问题:您尝试在 C 文件中包含 vector,所以它不起作用。

【讨论】:

  • 我更新了我的问题。我已经有了带有 APP_STL 的 Application.mk 文件:= gnustl_static
  • 更新了我看到的另一个问题的答案。
  • 在jni代码中有vector似乎很麻烦。所以我的问题是如何从返回向量或列表的c++函数中获得结果...任何准则,tnx
  • 你说你把向量包含在 second.c 中,所以它是 C 代码,不会编译。我每天在 Android 上的 C++ 中使用向量。
  • 这不是您发布的问题的范围。我建议您尝试将 second.c 重命名为 second.cpp,将所有 JNIEXPORT 声明为extern "C",如果可以再试一次。然后您可以针对具体情况提出另一个问题,因为 C++ 对象和 Java 对象之间没有直接关系,我们必须根据您的需要做一个特殊情况。
猜你喜欢
  • 2023-03-03
  • 2020-06-06
  • 1970-01-01
  • 2020-06-23
  • 2016-02-13
  • 2021-10-24
  • 1970-01-01
  • 2021-04-25
  • 2018-10-14
相关资源
最近更新 更多