반응형
출처 : http://xxw8393.blog.163.com/blog/static/37256834200910235723590/

Android NDK 已有 1.6,安装容易(解压便可),但使用上比较复杂,要与SDK的一起使用。NDK允许开发人员用C/C++语言编译部分的应用程序代码。

Linux 32/64-bit (x86)    android-ndk-1.6_r1-linux-x86.zip

增加 stlport, 可编译C/C++程序的代码为独立运行的 Android 应用程序

linux 방법
코드복사 
  1. cd ~/Android
  2. git clone git://umbel.mooo.com/ndk-wrappers.git

  3. export HOST=linux-x86
  4. export NDK_DIR=~/Android/android-ndk-1.6_r1
  5. export NDK_WRAPPERS_BASE=~/Android/ndk-wrappers
  6. export PATH=~/Android/ndk-wrappers/bin:$PATH

  7. 변경한다. ndk-wrappers/scripts/env-utils.sh
  8. SYSROOT="${NDK_DIR}/build/platforms/android-4/arch-arm"
  9. HOST=linux-x86

  10. 변경한다. ndk-wrappers/stlport/build/lib/android.mak
  11. --sysroot=$(NDK_DIR)/build/platforms/android-1.5/arch-arm \
  12. 으로 
  13. --sysroot=$(NDK_DIR)/build/platforms/android-4/arch-arm \

  14. cd ~/Android/ndk-wrappers
  15. ./env.sh
  16. ./setup.sh



STLPort 설치
코드복사 
  1. cd ~/Android/ndk-wrappers
  2. ./setup.sh


테스트 qsort.cpp
코드복사 
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;

  4. int sortFunction( const void *intOne, const void *intTwo);

  5. const int length = 10;  

  6. int main(void)
  7. {
  8.    int i,intArray[length];

  9.    for (i = 0; i < length; i++)
  10.    {
  11.       cout << "Enter a number [" << i << "]: ";
  12.       cin >> intArray[i];
  13.    }
  14.    cout << "\n";

  15.    qsort((void *)intArray, length, sizeof(intArray[0]), sortFunction);

  16.    for (i = 0; i < length; i++)
  17.       cout << "intArray [" << i << "]: " << intArray[i] << endl;

  18.    cout << "Done." << endl;
  19. return 0;
  20. }

  21. int sortFunction( const void *a, const void *b)
  22. {
  23.    int intOne = *((int*)a);
  24.    int intTwo = *((int*)b);
  25.    if (intOne < intTwo)
  26.       return -1;
  27.    if (intOne == intTwo)
  28.       return 0;
  29.    return 1;

  30. }


컴파일 및 테스트
코드복사 
  1. ~/Android/ndk-wrappers/bin/arm-linux-g++ -o qsort qsort.cpp -L~/Android/ndk-wrappers/stlport/build/lib/obj/arm-linux-gcc/so
  2. ~/Android/android-sdk-mac_x86-1.6_r1/tools/adb push qsort /data/local/bin/qsort
  3. ~//Android/android-sdk-mac_x86-1.6_r1/tools/adb shell chmod 755 /data/local/bin/qsort
  4. ~/Android/android-sdk-mac_x86-1.6_r1/tools/adb shell

  5. 进入shell 后
  6. /data/local/bin/qsort


android gcc toolchain

pocketmagic 有一篇关于编译 android gcc toolchain 的方法可编译C/C++程序的代码为独立运行的 Android 应用程序
http://www.pocketmagic.net/?p=682

现附上已编译好的mac (androidgcc_mac.tgz) 及 linux (androidgcc_linux.tgz) 版本
使用方法如下

(1) 下载后解压

코드복사
  1. mkdir -p ~/Android/androidgcc
  2. cd ~/Android/androidgcc
  3. tar -xzvf  ~/Documents/androidgcc_linux.tgz


(2) 下载agcc 脚本至 ~/Android/Projects/
http://www.pocketmagic.net/wp-content/uploads/2009/05/agcc

可用 mac / linux Terminal
코드복사
  1. cd ~/Android/Projects/
  2. curl -O http://www.pocketmagic.net/wp-content/uploads/2009/05/agcc


(3) agcc 安装方法

linux 用 Terminal 安装方法
复制代码
  1. cd ~/Android/Projects/
  2. chmod +x agcc


(4) 修改环境变量
linux
코드복사
  1. export PATH=~/Android/androidgcc/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin:${PATH}
  2. vi ~/.bash_profile
  3. PATH=~/Android/androidgcc/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin:~/Android/android-sdk-linux_x86-1.6_r1/tools:"${PATH}"


(5) 测试 Hello, Android World!
必须先将手机上的USB调试(Debug)模式打开及手机连接到 USB
코드복사 
  1. cd ~/Android/Projects/
  2. echo 'main() { printf("Hello, Android World!\n");}' > main.c
  3. ./agcc main.c -o main
  4. adb push main /data/local/bin/main
  5. adb shell chmod 775  /data/local/bin/main
  6. adb shell /data/local/bin/main


(6) 用 Makefile
코드복사
  1. AGCC=~/Android/androidgcc
  2. #mac
  3. SDKTOOL=~/Android/android-sdk-mac_x86-1.6_r1/tools
  4. #linux
  5. #SDKTOOL=~/Android/android-sdk-linux_x86-1.6_r1/tools
  6. APP=hello

  7. #mac
  8. CC=$(AGCC)/prebuilt/darwin-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc
  9. CPP=$(AGCC)/prebuilt/darwin-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++

  10. #linux
  11. #CC=$(AGCC)/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc
  12. #CPP=$(AGCC)/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++


  13. LD=$(CPP)

  14. CPPFLAGS += -I$(AGCC)/bionic/libstdc++/include

  15. CFLAGS += -I$(AGCC)/system/core/include
  16. CFLAGS += -I$(AGCC)/hardware/libhardware/include
  17. CFLAGS += -I$(AGCC)/hardware/ril/include
  18. CFLAGS += -I$(AGCC)/dalvik/libnativehelper/include
  19. CFLAGS += -I$(AGCC)/frameworks/base/include
  20. CFLAGS += -I$(AGCC)/external/skia/include
  21. CFLAGS += -I$(AGCC)/out/target/product/generic/obj/include
  22. CFLAGS += -I$(AGCC)/bionic/libc/arch-arm/include
  23. CFLAGS += -I$(AGCC)/bionic/libc/include
  24. CFLAGS += -I$(AGCC)/bionic/libc/kernel/common
  25. CFLAGS += -I$(AGCC)/bionic/libc/kernel/arch-arm
  26. CFLAGS += -I$(AGCC)/bionic/libm/include
  27. CFLAGS += -I$(AGCC)/bionic/libm/include/arch/arm
  28. CFLAGS += -I$(AGCC)/bionic/libthread_db/include
  29. CFLAGS += -I$(AGCC)/bionic/libm/arm
  30. CFLAGS += -I$(AGCC)/bionic/libm
  31. CFLAGS += -I$(AGCC)/out/target/product/generic/obj/SHARED_LIBRARIES/libm_intermediates
  32. CFLAGS += -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DSK_RELEASE -DNDEBUG
  33. CFLAGS += -include $(AGCC)/system/core/include/arch/linux-arm/AndroidConfig.h
  34. CFLAGS += -UDEBUG -march=armv5te -mtune=xscale -msoft-float -mthumb-interwork -fpic -fno-exceptions -ffunction-sections -funwind-tables -fstack-protector -fmessage-length=0 -Bdynamic

  35. LDFLAGS += -Wl,-T,$(AGCC)/build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -Wl,--no-undefined -Wl,-rpath-link=$(AGCC)/out/target/product/generic/obj/lib

  36. LDFLAGS += -L$(AGCC)/out/target/product/generic/obj/lib -nostdlib $(AGCC)/out/target/product/generic/obj/lib/crtend_android.o $(AGCC)/out/target/product/generic/obj/lib/crtbegin_dynamic.o $(AGCC)/prebuilt/darwin-x86/toolchain/arm-eabi-4.2.1/lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a

  37. LDFLAGS += -lutils -lc -lm -lstdc++

  38. all:    $(APP)

  39. OBJS += $(APP).o

  40. $(APP):    $(OBJS)
  41.     $(LD) $(LDFLAGS) -o $@ $^

  42. %.o:    %.c
  43.     $(CC) -c $(CFLAGS) $< -o $@

  44. %.o:    %.cpp
  45.     $(CPP) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

  46. install: $(APP)
  47.     $(SDKTOOL)/adb push $(APP) /data/local/bin/$(APP)
  48.     $(SDKTOOL)/adb shell chmod 755 /data/local/bin/$(APP)

  49. run:
  50.     $(SDKTOOL)/adb shell /data/local/bin/$(APP)

  51. clean:
  52.     @rm -f $(APP).o $(APP)


hello.cpp
코드복사
  1. #include <stdio.h>
  2. #include <utils/String8.h>
  3. using namespace android;

  4. int main()
  5. {
  6.    String8 str = String8("Hello Android\n");
  7.    str += "Hello C++\n";
  8.    str.append("\n... Android gcc toolchain ...\n");
  9.    printf(str);
  10.    return 0;
  11. }


테스트 hello.cpp
코드복사
  1. make APP=hello
  2. make APP=hello install run




내가 중국싸이트에서 보고 그대로 다운받아서 실습해본것을 압축한 파일을 올립니다.
반응형

'Android' 카테고리의 다른 글

JAVA Native Method (JNI)  (0) 2010.02.09
Mount a filesystem read-write  (0) 2010.02.09
cp on Android  (0) 2010.02.09
Posted by Real_G