第一步:
翻墙去https://developer.android.com/ndk/downloads/?nav=true下载NDK开发包
我选择的版本是r15c
选择一个合适的位置进行解压:
~/android/android-ndk/android-ndk-r15c
第二步:
编写一个测试文件main.c
#include <stdio.h>
int main(int argc, int **argv[]){
printf("Hello ARM!\n");
return 0;
}
第三步:
编写makefile文件
NDK_ROOT=/home/dhg/android/android-ndk/android-ndk-r15c
TOOLCHAINS_ROOT=$(NDK_ROOT)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
TOOLCHAINS_PREFIX=$(TOOLCHAINS_ROOT)/bin/arm-linux-androideabi
TOOLCHAINS_INCLUDE=$(TOOLCHAINS_ROOT)/lib/gcc/arm-linux-androideabi/4.9.x/include-fixed
PLATFORM_ROOT=$(NDK_ROOT)/platforms/android-19/arch-arm
PLATFORM_INCLUDE=$(PLATFORM_ROOT)/usr/include
PLATFORM_LIB=$(PLATFORM_ROOT)/usr/lib
MODULE_NAME=leyi
BUILD_TYPE=c
PATH_ANDROID=/data/local/tmp/
RM=rm
FLAGS=-I$(TOOLCHAINS_INCLUDE) \
-I$(PLATFORM_INCLUDE) \
-L$(PLATFORM_LIB) \
-nostdlib \
-lgcc \
-Bdynamic \
-lc \
-pie -fPIE
OBJS=$(MODULE_NAME).o \
$(PLATFORM_LIB)/crtbegin_dynamic.o \
$(PLATFORM_LIB)/crtend_android.o
all:
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -c $(MODULE_NAME).$(BUILD_TYPE) -o $(MODULE_NAME).o
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -S $(MODULE_NAME).$(BUILD_TYPE) -o $(MODULE_NAME).S
$(TOOLCHAINS_PREFIX)-gcc $(FLAGS) $(OBJS) -o $(MODULE_NAME)
clean:
$(RM) *.o
$(RM) *.S
install:
adb push $(MODULE_NAME) $(PATH_ANDROID)
adb shell chmod 755 $(PATH_ANDROID)$(MODULE_NAME)
adb shell $(PATH_ANDROID)$(MODULE_NAME)
第四步:
编译make
安装到手机make install
网友评论