ndk学习8: 编译动态库
目录:
手工编译动态库
ndk-build编译动态库(Eclipse环境)
手工编译静态库
老规矩还是先手工操作,知其然并知其所以然
需要用到的核心命令:
gcc -g -c -fpic -Wall mod1.c mod2.c mod3.c
gcc -g -shared mod1.o mod2.o mod3.o -o libfoo.so
gcc -g -fpic -Wall -shared mod1.o mod2.o mod3.o -o libfoo.so
程序运行会优先从系统目录找共享库,如果找不到才会去LD_LIBRARY_PATH环境变量中搜索
编译前的准备工作:
目标:我们打算将如上4个c文件编译成一个可执行文件,并使用
编写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) -Wall -fpic -g -c mod1.c mod2.c mod3.c
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -g -shared mod1.o mod2.o mod3.o -o libDemo.so
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -g -c $(MODALE_NAME).c
$(TOOLCHAINS_PREFIX)-gcc -g -Wall $(FLAGS) $(OBJS) libDemo.so -o $(MODALE_NAME)
rm -f *.o
#删除所有.o文件
clean:
# 使用cmd命令 用del
rm -f *.o
#安装程序到手机
install:
adb push $(MODALE_NAME) /data/local/tmp
adb push libDemo.so /data/local/tmp
adb shell chmod 755 /data/local/tmp/$(MODALE_NAME)
# adb shell export LD_LIBRARY_PATH=/data/local/tmp
adb shell /data/local/tmp/$(MODALE_NAME)
#运行程序
run:
adb shell /data/local/tmp/$(MODALE_NAME)
编译:
输入make编译
运行:
输入make install安装尝试运行
报错: 找不到共享库
由于export只能用于当前shell 所以我没有没法在make file里面使用
或者你当前环境root,配置全局的LD_LIBRARY_PATH环境变量也可以
解决方案:
进入adb shell执行
export LD_LIBRARY_PATH=/data/local/tmp
echo $LD_LIBRARY_PATH
/data/local/tmp/hello
用Eclipse编译共享库
修改android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := demo
LOCAL_SRC_FILES := mod1.cpp mod2.cpp mod3.cpp
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Hello
LOCAL_SRC_FILES := Hello.cpp
LOCAL_SHARED_LIBRARIES := demo
include $(BUILD_EXECUTABLE)
LOCAL_SRC_FILES := mod1.cpp mod2.cpp mod3.cpp 指明编译源码文件
BUILD_SHARED_LIBRARY 指明编译的目标为共享库
LOCAL_SHARED_LIBRARIES := demo 在编译的可执行对象中加入共享库
在工程目录下新建makefile:
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)
run_x86_share:
adb push .\libs\x86\$(MODALE_NAME) /data/local/tmp
adb push .\libs\x86\libdemo.so /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_share安装运行
同上面的方法一样:
adb shell
export LD_LIBRARY_PATH=/data/local/tmp
echo $LD_LIBRARY_PATH
/data/local/tmp/Hello