NDK学习之静态库的编译与调用
扫描二维码
随时随地手机看文章
源代码全放在新建的jni目录
编译的代码 PrintTest.c
内容:
Print.H:
extern int Add(int x, int y);
Print.c:
#include "PrintTest.h" int Add(int x, int y) { return x + y; }
Android.mk:
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libprint LOCAL_SRC_FILES := PrintTest.c include $(BUILD_STATIC_LIBRARY)
Application.mk:
APP_ABI := armeabi
cygwin编译:ndk-build
objlocal下生成libprint.a文件 ,保存好
调用:
在一个动态库中调用他
文件为Use.c:
#include "PrintTest.h" #includeint UseTest( int x , int y ) { return Add(x, y); }
Application.mk:
LOCAL_PATH:= $(call my-dir) # 需要把静态库导入 # include $(CLEAR_VARS) LOCAL_MODULE := libprint LOCAL_SRC_FILES := libprint.a include $(PREBUILT_STATIC_LIBRARY) # 第二个为动态库,在动态库中使用我们编译的静态库 include $(CLEAR_VARS) LOCAL_MODULE := libuse LOCAL_SRC_FILES := Use.c LOCAL_STATIC_LIBRARIES := libprint include $(BUILD_SHARED_LIBRARY)
libprint.a文件移动到jni目录
ndk-build:
Administrator@momo-PC /cygdrive/d/cocos/android-ndk-r10c/mycode1
$ ndk-build
[armeabi] Compile thumb : use <= Use.c
[armeabi] SharedLibrary : libuse.so
[armeabi] Install : libuse.so => libs/armeabi/libuse.so
编译成功