The toolchain in ndk R5 can finally support the makefile compilation system. However, it takes a lot of effort to support GNU libc ++, and the documentation is too lacking. The following is a simple successful case:
First, we will write a simple C ++ program named test. cpp, which contains the basic features of iostream, vector, typeid, and libc ++:
# Include <vector> <br/> # include <iostream> <br/> # include <typeinfo> <br/> Class A <br/>{< br/> public: <br/> virtual void func () <br/> {<br/> STD: cout <"A" <STD: Endl; <br/>}< br/>}; <br/> Class B: public a <br/>{< br/> Public: <br/> virtual void func () <br/>{< br/> STD: cout <"B" <STD: Endl; <br/>}< br/> }; <br/> int main (void) <br/>{< br/> B * B = new B (); <br/> A * A = dynamic_cast <A *> (B); <br/> if (a) <br/>{< br/> STD :: cout <"Calling A: func ()" <STD: Endl; <br/> A-> func (); <br/> STD :: cout <"typeid =" <typeid (* ). name () <STD: Endl; <br/>}< br/> else <br/> {<br/> STD: cerr <"dynamic_cast error. "<STD: Endl; <br/>}< br/> STD: vector <int> V; <br/> v. push_back (1); <br/> STD: cout <v. size () <STD: Endl; <br/> return 0; <br/>}< br/>
Then let's write our makefile:
Ndk_home =/developer/android-ndk-r5 <br/> toolchain_home = $(ndk_home)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86 <br/> cross_compile = $ (toolchain_home) /bin/ARM-Linux-androideabi-<br/> sysroot = $ (ndk_home)/platforms/Android-3/arch-arm <br/> cc = $ (cross_compile) g ++ <br/> stl_home = $ (ndk_home)/sources/cxx-STL/gnu-libstdc ++ <br/> ndk_lib =$ (sysroot) /usr/lib <br/> # ccflags =-MMD-MP-FPIC-ffunction-sections-funwind-tables-fstack-protector-d1_arm_arch_5 _-d1_arm_arch_5t _-1 _ _-d1_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 <br/> ccflags = <br/> incdirs + =-I $ (sysroot)/usr/include <br/> incdirs + =-I $ (stl_home) /include <br/> incdirs + =-I $ (stl_home)/libs/armeabi/include <br/> ldflags + = -- sysroot = $ (sysroot) -L $ (ndk_lib) <br/> # ldflags + =-wl, -- GC-sections-wl,-Z, nocopyreloc-wl, -- no-undefined-wl, -Z, noexecstack-wl,-rpath-link =$ (sysroot) /usr/lib <br/> # ldflags + =-lsupc ++-lstdc ++ <br/> # ldlibs + =$ (toolchain_home) /ARM-Linux-androideabi/lib/libsupc ++. A <br/> # ldlibs + = $ (toolchain_home)/lib/GCC/ARM-Linux-androideabi/4.4.3/libgcc. A <br/> # ldlibs + = $ (sysroot)/usr/lib/libc. A <br/> ldlibs + = $ (stl_home)/libs/armeabi/libstdc ++. A <br/> output = test <br/> Hello: test. O <br/> $ (CC) $ (ldflags) test. o $ (ldlibs)-o $ (output) <br/> test. o: test. CPP <br/> $ (CC) $ (ccflags) $ (incdirs)-C test. CPP-O test. O <br/> clean: <br/> RM-f *. O <br/>
Make, compiled successfully, and uploaded to emulator to run the result:
Calling a: func ()
B
Typeid = 1b
1
Normal.