【发布时间】: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