Use C + + in NDK learning 5:NDK

Source: Internet
Author: User


The NDK does not support standard C + + libraries, exceptions, RTTI, etc. by default

?

Detailed introduction to C + + support in the NDK documentation

?

I. Using the C + + standard library

Introduced:

The default is to use the minimum amount of C + + Runtime library, add App_stl in application.mk to indicate the required library

Note that the following shared libraries may not be available on the target phone or emulator, and you need to use it as a static library

Support for various libraries in the NDK

?

The difference between Ps:stlport and Gnustl

? ?? The reason Android NDK does not provide STL is due to copyright issues. Because the standard GNU STL is provided by libstdc++, although it is a GPL itself, it is free to use as long as it does not modify its code. On the Android platform, because of a lot of adaptation problems, the libstdc++ can not be used directly, so the NDK is not directly available. STLport does not have such limitations, so it is a better alternative solution

?

?

Use stlport:

to use STLport For example :

?

1.application.mk Join in

app_stl?:=?stlport_static ?

? ? PS: Some mobile phone or simulator may not have stlport_shared Library , the runtime may error

?

Writing code :?

#include? <iostream>

#include? <stdio.h>

using? namespacestd;

int? Main (int? argc,? Char*?argv[])? {

???? cout? <<? "Hello?" World "? <<? Endl;

???? return0;

}?

?

when you are finished compiling :

?

STLport need to include STLport The library came in

Android-ndk-r10b\sources\cxx-stl the directory has a corresponding version of the STL

For example, we want to use STLport its header files are generally

E:\Android\android-ndk-r10b\sources\cxx-stl\stlport\stlport

Add the above path to the Paths and Symbols can be

??

Run successfully :

?

you can now use STL various data structures in the , like Map

#include? <map>

#include? <iostream>

#include? <string>

using? namespacestd;

int? Main (int? argc,? Char*?argv[])? {

???? map<int,string>?mapStudent;

???? Mapstudent.insert (map<int,? ) String;::value_type(1,"Bing1"));

???? Mapstudent.insert (map<int,? ) String;::value_type(2,"Bing2"));

???? Mapstudent.insert (map<int,? ) String;::value_type(3,"Bing3"));

???? Mapstudent.insert (map<int,? ) String;::value_type(4,"Bing4"));

???? map<int,? string;::iteratoriter;

???? for? (Iter?=?mapstudent.begin (); iter?! =?mapstudent.end (); iter++)? {

???????? cout?<<? (*iter). first <<? "?"? <<? (*iter). Second?<<?endl;

????}

???? return0;

}?

?

The operation results are as follows :

?

?

two . using Exceptions

?

some points to be aware of :

1. C + + exception is supported after NDK 5

2. You can add usage exceptions in android.mk and application.mk

? ? difference is android.mk is local.

? ?? The Application.mk is a global

?

You can use it after you add it

#include? <iostream>

using? namespacestd;

int? Main (int? argc,? Char*?argv[])? {

???? try? {

???????? cout?<<? "Hello?" World "?<<?endl;

????}? catch? (...)? {

???????? cout?<<? "Error"?<<?endl;

????}

???? return0;

}?

?

Run :

?

?

?

three . Use RTTI

Same as Exception , no more introductions.

?

Code :

#include? <iostream>

#include? <typeinfo>

using? namespacestd;

class? Cnumber

{

};

int? Main (int? argc,? Char*?argv[])

{

???? cnumber? nnum?;

???? cout?<<? typeID (nnum). Name ()?<<?endl;

???? cout?<<? "Hello?" World "?<<?endl;

}?

?

Run :

?

?

?

?

?

?

?

?

?

?

?

Use C + + in NDK learning 5:NDK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.