- 独立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