NDK r5 Toolchain 简单测试

NDK r5 带的 toolchain 终于可以支持 makefile 的编译系统了。不过为了让它支持 GNU libc++ 还颇费了一番功夫,文档实在太缺乏了。下面是一个简单的试成功了的 case:

 

首先我们写一个简单的 C++ 程序,这里命名为 test.cpp,里面用到了 iostream, vector, typeid,libc++ 的基本特性都包含在里面了:

#include <vector> #include <iostream> #include <typeinfo> class A { public: virtual void func() { std::cout << "A" << std::endl; } }; class B : public A { public: virtual void func() { std::cout << "B" << std::endl; } }; int main(void) { B * b = new B(); A * a = dynamic_cast<A*>(b); if (a) { std::cout << "calling A::func()" << std::endl; a->func(); std::cout << "typeid=" << typeid(*a).name() << std::endl; } else { std::cerr << "dynamic_cast error." << std::endl; } std::vector<int> v; v.push_back(1); std::cout << v.size() << std::endl; return 0; }

然后我们来写我们的 makefile:

NDK_HOME=/Developer/android-ndk-r5 TOOLCHAIN_HOME=$(NDK_HOME)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86 CROSS_COMPILE=$(TOOLCHAIN_HOME)/bin/arm-linux-androideabi- SYSROOT=$(NDK_HOME)/platforms/android-3/arch-arm CC=$(CROSS_COMPILE)g++ STL_HOME=$(NDK_HOME)/sources/cxx-stl/gnu-libstdc++ NDK_LIB=$(SYSROOT)/usr/lib #CCFLAGS=-MMD -MP -fpic -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -DANDROID -Wa,--noexecstack -g CCFLAGS= INCDIRS += -I$(SYSROOT)/usr/include INCDIRS += -I$(STL_HOME)/include INCDIRS += -I$(STL_HOME)/libs/armeabi/include LDFLAGS += --sysroot=$(SYSROOT) -L$(NDK_LIB) #LDFLAGS += -Wl,--gc-sections -Wl,-z,nocopyreloc -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-rpath-link=$(SYSROOT)/usr/lib #LDFLAGS += -lsupc++ -lstdc++ #LDLIBS += $(TOOLCHAIN_HOME)/arm-linux-androideabi/lib/libsupc++.a #LDLIBS += $(TOOLCHAIN_HOME)/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a #LDLIBS += $(SYSROOT)/usr/lib/libc.a LDLIBS += $(STL_HOME)/libs/armeabi/libstdc++.a OUTPUT=test hello: test.o $(CC) $(LDFLAGS) test.o $(LDLIBS) -o $(OUTPUT) test.o: test.cpp $(CC) $(CCFLAGS) $(INCDIRS) -c test.cpp -o test.o clean: rm -f *.o

 

make,编译成功,上传到 emulator 里面运行结果:

 

calling A::func()
B
typeid=1B
1

 

正常。

你可能感兴趣的:(vector,Class,文档,iostream,makefile,output)