目錄: linux
手工編譯動態庫 android
ndk-build編譯動態庫(Eclipse環境) shell
手工編譯靜態庫 windows
老規矩仍是先手工操做,知其然並知其因此然 tcp
須要用到的核心命令: ide
gcc -g -c -fpic -Wall mod1.c mod2.c mod3.c ui
gcc -g -shared mod1.o mod2.o mod3.o -o libfoo.so idea
gcc -g -fpic -Wall -shared mod1.o mod2.o mod3.o -o libfoo.so spa
程序運行會優先從系統目錄找共享庫,若是找不到纔會去LD_LIBRARY_PATH環境變量中搜索 debug
編譯前的準備工做:
目標:咱們打算將如上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