把NDK的工具鏈提取出來單獨使用

weixin_34015860發表於2016-05-06
  • 獨立toolchain

      把NDK壓縮包解壓到系統,如/mnt目錄下,後在/mnt目錄下建立資料夾my_ndk_toolchain,然後再/mnt目錄下執行以下命令:
/mnt/android-ndk-r9c/build/tools/make-standalone-toolchain.sh --platform=android-19 --toolchain=arm-linux-androideabi-4.8 --stl=stlport --install-dir=/mnt/my_ndk_toolchain
出現以下列印:
dir=/mnt/my_ndk_toolchain
Copying prebuilt binaries...
Copying sysroot headers and libraries...
Copying libstdc++ headers and libraries...
Copying files to: /mnt/my_ndk_toolchain
Cleaning up...
Done.
說明獨立的工具鏈成功,對執行的命令進行簡單說明:
/mnt/android-ndk-r9c/build/tools/make-standalone-toolchain.sh:執行NDK目錄下make-standalone-toolchain.sh指令碼;
--platform:指工具鏈將使用哪個版本的Android API,可cd /mnt/android-ndk-r9c/platform中檢視,我這裡使用的是Android-19;
--toolchain:指獨立出來的工具鏈哪種用途的編譯,arm(arm-linux-androideabi-4.8),X86(x86-4.8)或MIPS(mipsel-linux-android-4.8),可cd toolchains中檢視並選擇適合的型別,我這裡使用的是嵌入式;
--stl:指工具鏈支援C++ stl,stlport代表C++庫將靜態連結,stlport_shared將動態連結;
--install-dir:指安裝目錄;
注意:因為我使用的是32-bit Ubuntu,獨立工具鏈預設是32位,所以在引數中沒有指定系統型別,如果是64-bit Linux系統,需加入--system=linux-x86_64 或MacOSX加入--system=darwin-x86_64。

  • 測試程式

hello.cpp
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
    std::string str = "hello, ndk! this is my own toolchain! ^-^";
    std::cout << str << std::endl;
    return 0;
}
Makefile
rm=/bin/rm -f
CC=/mnt/my_ndk_toolchain/bin/arm-linux-androideabi-g++
PROGNAME = main
INCLUDES= -I.
CFLAGS  = $(INCLUDES) -g -fPIC -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE
OBJS   = hello.o
LDFLAGS =
all :$(PROGNAME)
%.o: %.cpp
        $(CC) $(CFLAGS) -c -o $@ $<
$(PROGNAME) : $(OBJS)
        @echo  "Linking $(PROGNAME)......"
        ${CC} ${LDFLAGS} -o ${PROGNAME} ${OBJS}
        @echo  "Linking Success!"
clean:
        $(rm) *.o  $(PROGNAME)


編譯後得到可執行檔案:main,adb push到嵌入式Android平臺後,./main執行,得到以下結果:
root@android :/data # ./main                                                
hello, ndk! this is my own toolchain! ^-^

 

 

 

 

 

 

 

Generate a customized Android toolchain installation that includes
a working sysroot. The result is something that can more easily be
used as a standalone cross-compiler, e.g. to run configure and
make scripts.

Valid options (defaults are in brackets):

  --help                   Print this help.
  --verbose                Enable verbose mode.
  --dryrun                 Set to dryrun mode.
  --toolchain=<name>       Specify toolchain name
  --use-llvm               Use LLVM.
  --stl=<name>             Specify C++ STL [gnustl]
  --arch=<name>            Specify target architecture
  --abis=<list>            Specify list of target ABIs.
  --ndk-dir=<path>         Take source files from NDK at <path> [.]
  --package-dir=<path>     Place package file in <path> [/tmp/ndk-root]
  --install-dir=<path>     Don't create package, install files to <path> instead.
  --platform=<name>        Specify target Android platform/API level. [android-3]

  

'-march='  armv2 armv2a armv3 armv3m armv4 armv4t armv5 armv5e armv5t armv5te armv6 armv6-m armv6j armv6k armv6s-m armv6t2 armv6z armv6zk armv7 armv7-a armv7-m armv7-r armv7e-m armv7ve armv8-a armv8-a+crc iwmmxt iwmmxt2 native

 

 

 

arm-linux-androideabi-gcc 
-MMD -MP-MF ./obj/local/armeabi-v7a/objs/usb100_static/libusb/hotplug.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes 
-march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -O2 -g -DNDEBUG -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300 
-Ijni/libusb/android/jni/../../ -Ijni/libusb/android/jni/../../libusb -Ijni/libusb/android/jni/../../libusb/os -Ijni/libusb/android/jni/../../../ -Ijni/libusb/android/jni/../.. -DANDROID -Ijni/libusb/android/jni/../../ -Ijni/libusb/android/jni/../../libusb -Ijni/libusb/android/jni/../../libusb/os -Ijni/libusb/android/jni/../../../ -DANDROID_NDK -DLOG_NDEBUG -DACCESS_RAW_DESCRIPTORS -O3 -fstrict-aliasing -fprefetch-loop-arrays-Wa,--noexecstack -Wformat -Werror=format-security -IE:/Android_SDK/android-ndk-r10d/platforms/android-14/arch-arm/usr/include -c jni/libusb/android/jni/../../libusb/hotplug.c -o ./obj/local/armeabi-v7a/objs/usb100_static/libusb/hotplug.o





arm-linux-androideabi-g++
-Wl,-soname,libusb100.so 
-shared --sysroot=E:/Android_SDK/android-ndk-r10d/platforms/android-14/arch-arm -Wl,--whole-archive ./obj/local/armeabi-v7a/libusb100_static.a 
-Wl,--no-whole-archive -lgcc -no-canonical-prefixes 
-march=armv7-a -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -mthumb -LE:/Android_SDK/android-ndk-r10d/platforms/android-14/arch-arm/usr/lib -llog -lc -lm -o ./obj/local/armeabi-v7a/libusb100.so

  

 

Attribute Section: aeabi
File Attributes
Tag_CPU_name: "ARM v7"
Tag_CPU_arch: v7
Tag_CPU_arch_profile: Application
Tag_ARM_ISA_use: Yes
Tag_THUMB_ISA_use: Thumb-2
Tag_FP_arch: VFPv3
Tag_Advanced_SIMD_arch: NEONv1
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align_needed: 8-byte
Tag_ABI_enum_size: int
Tag_ABI_HardFP_use: SP and DP
Tag_ABI_optimization_goals: Aggressive Speed
Tag_CPU_unaligned_access: v6
Tag_DIV_use: Not allowed

相關文章