Android 编译成静态编译库a以及调用实例
一、编译静态库。
1、Amdroid.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE:=libtest_static
LOCAL_SRC_FILES := helloworld.c
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS += -DRIL_SHLIB
include $(BUILD_STATIC_LIBRARY)
2、helloworld.c
#include <stdio.h>
#include <string.h>
#include <utils/Log.h>
#include "helloworld.h"
int android_function(int a,int b)
{
int ret=0;
ret=a+b;
ALOGD("android_function a+b=%d ret=%d \n",ret,ret);
return ret;
}
3、helloworld.h
#include <stdio.h>
#include <string.h>
#include <utils/Log.h>
int android_function(int a,int b);
4、编译mmm hardware/rockchip/static_library/,生成out/target/product...rm/STATIC_LIBRARIES/libtest_static_intermediates/libtest_static.a文件。
二、调用静态库
1、Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE:=call_static
LOCAL_SHARED_LIBRARIES:=liblog libc libcutils
LOCAL_STATIC_LIBRARIES:=libtest_static
LOCAL_LDFLAGS := $(LOCAL_PATH)/libtest_static.a
LOCAL_SRC_FILES :=call_static.c
LOCAL_LDLIBS := -llog
include $(BUILD_EXECUTABLE)
2、call_static.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <utils/Log.h>
#include <stdbool.h>
#include <stdlib.h>
#include <cutils/properties.h>
#include "helloworld.h"
#define LOG_TAG "CALL STATIC"
int main()
{
ALOGD("*************begin*************");//
android_function(8,9);
ALOGD("*************end*************");
return 0;
}
3、把libtest_static.a 文件copy过来到根目录。
4、编译mmm hardware/rockchip/call_static/
三、adb push call_static /system/bin/ 执行验证
四,所用到的文件,下载路径https://download.csdn.net/download/qq_37858386/13082934
五、LOCAL_SHARED_LIBRARIES和LOCAL_STATIC_LIBRARIES是引入系统的,LOCAL_LDFLAGS是引用第三方的。参考https://blog.csdn.net/qq840727854/article/details/54782700?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param