目录:
手工编译静态库
ndk-build编译静态库
手工编译静态库
老规矩还是先手工操作,知其然并知其所以然
需要用到的核心命令:
gcc –g –c mod1.c mod2.c mod3.c 编译obj (debug版)
AR r libdemo.a mod1.o mod2.o mod3.o 将3个obj 添加到libdemo.a中
AR tv libdemo.a 查看libdemo.a中的obj文件
AR d libdemo.a mod3.o 将mod3.o从libdemo.a中删除
于是有编译静态库的流程:
gcc------> obj 文件---->ar----->静态库文件
编译前的准备工作:
目标:我们打算将如上4个c文件编译成一个可执行文件,并使用
使用ndk环境编译静态库:
环境太多,为了复用性!我们编写makefile脚本
#ndk根目录
NDK_ROOT=E:\Android\android-ndk-r10b
#编译器根目录
TOOLCHAINS_ROOT=$(NDK_ROOT)/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64
#编译器目录
TOOLCHAINS_PREFIX=$(TOOLCHAINS_ROOT)/bin/arm-linux-androideabi
#头文件搜索路径
TOOLCHAINS_INCLUDE=$(TOOLCHAINS_ROOT)/lib/gcc/arm-linux-androideabi/4.6/include-fixed
#SDK根目录
PLATFROM_ROOT=$(NDK_ROOT)/platforms/android-14/arch-arm
#sdk头文件搜索路径
PLATFROM_INCLUDE=$(PLATFROM_ROOT)/usr/include
#sdk库文件搜索路径
PLATFROM_LIB=$(PLATFROM_ROOT)/usr/lib
#文件名称
MODALE_NAME=hello
#删除
RM=del
#编译选项
FLAGS=-I$(TOOLCHAINS_INCLUDE) \
-I$(PLATFROM_INCLUDE) \
-L$(PLATFROM_LIB) \
-nostdlib \
-lgcc \
-Bdynamic \
-lc
#所有obj文件
OBJS=$(MODALE_NAME).o \
$(PLATFROM_LIB)/crtbegin_dynamic.o \
$(PLATFROM_LIB)/crtend_android.o
#编译器链接
all:
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -g -c mod1.c mod2.c mod3.c
$(TOOLCHAINS_PREFIX)-ar r libdemo.a mod1.o mod2.o mod3.o
$(TOOLCHAINS_PREFIX)-ar tv libdemo.a
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -c $(MODALE_NAME).c -o $(MODALE_NAME).o
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -o $(MODALE_NAME) $(OBJS) libdemo.a
$(RM) *.o
#删除所有.o文件
clean:
$(RM) *.o
#安装程序到手机
install:
adb push $(MODALE_NAME) /data/local/tmp
adb shell chmod 755 /data/local/tmp/$(MODALE_NAME)
adb shell /data/local/tmp/$(MODALE_NAME)
#运行程序
run:
adb shell /data/local/tmp/$(MODALE_NAME)
我们直接在使用make编译:
磁盘目录下生成:
hello 可执行程序
libdemo.a 静态库文件
使用make install安装运行程序:
3个mod成功打印出来
Eclipse下使用静态库
把上面的代码拷到Eclipse工程:
修改Android.mk如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mod1
LOCAL_SRC_FILES := mod1.cpp
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := mod2
LOCAL_SRC_FILES := mod2.cpp
LOCAL_STATIC_LIBRARIES := mod1
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := mod3
LOCAL_SRC_FILES := mod3.cpp
LOCAL_STATIC_LIBRARIES := mod2
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Hello
LOCAL_SRC_FILES := Hello.cpp
LOCAL_STATIC_LIBRARIES := mod3
include $(BUILD_EXECUTABLE)
编译生成:
运行脚本:
MODALE_NAME := Hello
# x86 path
X86_TOOLS_PATH :=E:\Android\android-ndk-r10b\toolchains\x86-4.6\prebuilt\windows-x86_64\bin
X86_GDB_PATH := $(X86_TOOLS_PATH)\i686-linux-android-gdb.exe
X86_GDB_SERVER := E:\Android\android-ndk-r10b\prebuilt\android-x86\gdbserver\gdbserver
# arm-linux-androideabi-4.6 path
arm_tools_path :=E:\Android\android-ndk-r10b\toolchains\arm-linux-androideabi-4.6\prebuilt\windows-x86_64\bin
arm_4_6_path := $(arm_tools_path)\arm-linux-androideabi-gdb.exe
arm_gdb_server :=E:\Android\android-ndk-r10b\prebuilt\android-arm\gdbserver\gdbserver
run_arm:
adb push .\libs\armeabi-v7a\$(MODALE_NAME) /data/local/tmp
adb shell chmod 755 /data/local/tmp/$(MODALE_NAME)
adb shell /data/local/tmp/$(MODALE_NAME)
run_x86:
adb push .\libs\x86\$(MODALE_NAME) /data/local/tmp
adb shell chmod 755 /data/local/tmp/$(MODALE_NAME)
adb shell /data/local/tmp/$(MODALE_NAME)
debug_x86:
adb forward tcp:12345 tcp:12345
adb push $(X86_GDB_SERVER) /data/local/tmp
adb shell chmod 777 /data/local/tmp/gdbserver
adb push .\obj\local\x86\$(MODALE_NAME) /data/local/tmp
adb shell chmod 777 /data/local/tmp/$(MODALE_NAME)
adb shell /data/local/tmp/gdbserver :12345 /data/local/tmp/$(MODALE_NAME)
client_x86:
$(X86_GDB_PATH) .\obj\local\x86\$(MODALE_NAME)
# 1. target remote localhost:12345
# 2. gdb.setup
执行make run_x86即可成功执行: