在 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);
}
安卓.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: 错误:预期'=' , ',', ';', 'asm' 或 ' attribute ' 在 '<' 标记 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont 之前。 h:14:1: 错误:在 '<' 标记 /home/username/dev/ndk/android-ndk-r9/sources/ 之前需要 '='、','、';'、'asm' 或 ' attribute ' cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1: 错误:在 '<' 标记 /home/username/ 之前需要 '='、','、';'、'asm' 或 ' attribute ' dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1:错误:预期的 '='、','、';'、'asm' 或 '属性'在'<'令牌/home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21:1之前:错误:预期'=',', '、';'、'asm' 或 ' attribute ' 在 '<' 标记之前 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_relops_cont.h:21 :1: 错误:在 '<' 标记 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl 之前需要 '='、','、';'、'asm' 或 ' attribute ' /stlport/stlport/stl/_relops_cont.h:24:1: 错误: 预期 '=', ',', ';', 'asm' 或 '属性' 在 '<' 标记之前 在 /home/username/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:错误:预期'=',',',';','<' 标记 /home/username/dev/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport/stl/_vector.h:760:10 之前的'asm' 或 ' attribute ':错误:预期'='、','、';'、'asm' 或 ' attribute ' 在 '<' 标记之前
在 Application.mk 中
APP_STL := stlport_static