windows下NDK开发调用动态库里的函数报错没有定义
是这样的,我用NDK编译C代码想在ANDROID手机上运行。但是在调用一些动态库的时候出现了一些问题,让我很头痛…… 一个多星期了,也找不到是什么原因。有个简单的例子。
main.cc 文件:
//#include <cstdio>
//#include <cstdlib>
//#include <stdexcept>
#include <android/log.h>
#include <stdio.h>
#include "add.h"
int main(int argc, char* argv[])
{
//System.loadLibrary("add");
int a=3;
int b=5;
printf("a+b = %d \n",add(a,b));
printf("Hello Android! \n");
return 0;
}
add.h文件:
int add(int a,int b);
add.c 文件:
#include <stdio.h>
int add(int a,int b)
{
return a+b;
}
Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := add
LOCAL_SRC_FILES := add.c
include $(BUILD_SHARED_LIBRARY)
#LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := cc
LOCAL_MODULE := th_test
LOCAL_SRC_FILES := main.cc
#LOCAL_LDLIBS := -L./ -lSecuTFMcApi
#LOCAL_LDLIBS := -llog -ladd
LOCAL_SHARED_LIBRARIES := -L./ -ladd
include $(BUILD_EXECUTABLE)
是想把add.c文件编译成一个.so(动态库文件)库里有add()函数,可以提供给main.cc文件所调用。可是在编译的时候报错:
$ $NDK/ndk-build
Compile thumb : add <= /android-ndk-r4-crystax/samples/th_test/jni/add.c
SharedLibrary : libadd.so
Install : libadd.so => /android-ndk-r4-crystax/samples/th_test/libs/armeabi
Compile++ thumb: th_test <= /android-ndk-r4-crystax/samples/th_test/jni/main.cc
make: *** 没有规则可以创建“/android-ndk-r4-crystax/samples/th_test/obj/local/armeabi/th_test”需要的目标“/android-ndk-r4-crystax/samples/th_test/obj/local/armeabi/lib-L./.so”。 停止。
如果把ANDROID.MK文件的LOCAL_SHARED_LIBRARIES := -L./ -ladd 改为:LOCAL_SHARED_LIBRARIES := add
报错:$ $NDK/ndk-build
Compile thumb : add <= /android-ndk-r4-crystax/samples/th_test/jni/add.c
SharedLibrary : libadd.so
Install : libadd.so => /android-ndk-r4-crystax/samples/th_test/libs/armeabi
Compile++ thumb: th_test <= /android-ndk-r4-crystax/samples/th_test/jni/main.cc
Executable : th_test
/android-ndk-r4-crystax/samples/th_test/obj/local/armeabi/objs/th_test/main..o: In function `main':
/android-ndk-r4-crystax/samples/th_test/jni/main.cc:18: undefined reference to `add(int, int)'
collect2: ld returned 1 exit status
/cygdrive/e/ANDROID/android-ndk-r4-crystax/build/core/build-executable.mk:38: recipe for target `/android-ndk-r4-crystax/samples/th_test/obj/local/armeabi/th_test' failed
make: *** [/android-ndk-r4-crystax/samples/th_test/obj/local/armeabi/th_test] Error 1
这是一个很简单的例子。但是我就头痛了一个多星期没有解开! 请大侠帮个忙。