[关闭]
@oro-oro 2018-03-28T16:19:00.000000Z 字数 2779 阅读 2732

一、实验环境准备

AndroidARM


1、Android SDK

2、GDB For Android

  1. $ adb push gdb /data/local/tmp/gdb
  2. $ adb shell
  3. # cd /data/local/tmp
  4. # chmod 755 gdb

3、NDK 环境搭建

3.1 Linux/Mac

具体用法可以用这个命令查看:make-standalone-toolchain.sh --help

  1. android-ndk$ ./build/tools/make-standalone-toolchain.sh --platform=android-L --toolchain=arm-linux-androideabi-4.9
  2. Copying prebuilt binaries...
  3. Copying sysroot headers and libraries...
  4. Copying c++ runtime headers and libraries...
  5. Creating package file: /tmp/ndk-bin/arm-linux-androideabi-4.9.tar.bz2
  6. Cleaning up...
  7. Done.
  8. $
  1. $ mkdir ~/toolchains
  2. $ cd ~/toolchains
  3. $ tar -xvjf /tmp/ndk-bin/arm-linux-androideabi-4.9.tar.bz2
  4. $ export PATH=~/toolchains/arm-linux-androideabi-4.9/bin:$PATH #设置PATH环境
  1. /* hello.c: Hello World program */
  2. #include <stdio.h>
  3. int main(int argc, char* argv[]){
  4. printf("Hello ARM World\n");
  5. return 0;
  6. }
  1. $ arm-linux-androideabi-gcc hello.c -o hello
  2. $ adb push hello /data/local/tmp/
  3. 2586 KB/s (1542488 bytes in 0.582s)
  4. $ adb shell /data/local/tmp/hello
  5. Hello ARM World!
  6. $

参考 android-ndk-r10d/docs/Programmers_Guide/html/md_3__key__topics__building__s_t_a_n_d_a_l_o_n_e-_t_o_o_l_c_h_a_i_n.html

3.2 Windows

  1. /* hello.c: Hello World program */
  2. #include <stdio.h>
  3. int main(int argc, char* argv[]){
  4. printf("Hello ARM World\n");
  5. return 0;
  6. }

makefile

  1. NDK_ROOT=D:\android-ndk-r10d
  2. TOOLCHAINS_ROOT=$(NDK_ROOT)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
  3. TOOLCHAINS_PREFIX=$(TOOLCHAINS_ROOT)/bin/arm-linux-androideabi
  4. TOOLCHAINS_INCLUDE=$(TOOLCHAINS_ROOT)/lib/gcc/arm-linux-androideabi/4.9/include-fixed
  5. PLATFORM_ROOT=$(NDK_ROOT)/platforms/android-19/arch-arm
  6. PLATFORM_INCLUDE=$(PLATFORM_ROOT)/usr/include
  7. PLATFORM_LIB=$(PLATFORM_ROOT)/usr/lib
  8. MODULE_NAME=hello
  9. RM=rm
  10. FLAGS=-I$(TOOLCHAINS_INCLUDE) \
  11. -I$(PLATFORM_INCLUDE) \
  12. -L$(PLATFORM_LIB) \
  13. -nostdlib \
  14. -lgcc \
  15. -Bdynamic \
  16. -lc
  17. OBJS=$(MODULE_NAME).o \
  18. $(PLATFORM_LIB)/crtbegin_dynamic.o \
  19. $(PLATFORM_LIB)/crtend_android.o
  20. all:
  21. $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -E $(MODULE_NAME).c -o $(MODULE_NAME).i
  22. $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -S $(MODULE_NAME).i -o $(MODULE_NAME).s
  23. $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -c $(MODULE_NAME).s -o $(MODULE_NAME).o
  24. $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) $(OBJS) -o $(MODULE_NAME)
  25. clean:
  26. $(RM) *.o
  27. install:
  28. adb push $(MODULE_NAME) /data/local/tmp/
  29. adb shell chmod 755 /data/local/tmp/$(MODULE_NAME)
  30. adb shell /data/local/tmp/$(MODULE_NAME)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注