在 Java 代码中:
System.loadLibrary("twolib-second");
int z = add(1, 2);
public native int add(int x, int y);
第一个.cpp:
#include "first.h"
int first(int x, int y) {
return x + y; }
第一.h:
#ifndef FIRST_H
#define FIRST_H
extern int first(int x, int y);
#endif /* FIRST_H */
第二个.c:
#include "first.h"
#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)
我不断收到此错误:
/home/用户名/ndk/android-ndk-r9/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../。 ./../../arm-linux-androideabi/bin/ld:./obj/local/armeabi/objs/twolib-second/second.o: 在函数 Java_com_example_jniexample_MainActivity_add:jni/second.c:26:
错误:未定义对“first”collect2的引用:ld返回1退出状态make:* [obj/local/armeabi/libtwolib-second.so]错误1