Import STL libraries to the Android NDK code
Posted by DimasTheDriver | Filed under Programming
This is a quick tip for those who are beginning to write native Android code. As one may have noticed, it isn’t possible to use containers like,string, vector, list inside the NDK samples. These are all part of the STL(Standard Template Library), and are expected to be available when writing C++ code.
To add STL to your NDK code, locate the Application.mk file inside your project’s jni folder. If it isn’t there, create it. Please note that the Application.mk is not theAndroid.mk file! The Android.mk file instructs the compiler and the JNI on how NDK code should be handled. The Application.mk, works similarly as the Android manifest file for your NDK code, allowing the programmer to add permissions and define other applications’ properties, like such as ‘enabling’ the STL support.
After creating the Application.mk, add this line of code:
- APP_STL := stlport_static
Now at the .c or .cpp file or at the header of the class where STL needs to be included, add the following:
- //to use strings
- #include <string>
- //to use vectors
- #include <vector>
- //and so on...
- /* add this line, to avoiding writing 'std::' every time a string (or any
- other container) is declared.*/
- using namespace std;
This was tested using Android NDK, Revision 5b, released in January 2011. I don’t know if works on previous version of the NDK.
댓글 없음:
댓글 쓰기