Preface
When testing the availability of the tinythread ++ library on different mobile platforms, several problems were encountered. Tinythread ++ is known as a thread library compatible with the c ++ 11 standard. It can be used when C ++ 11 is unavailable. In the future, C ++ 11 will become more popular and can be easily replaced.
Test status on different platforms:
- IOS: Pass. You need to rename the. M file that calls the C ++ code to the. MM file.
- WP: unknown. Not involved.
- Android: Pass. After solving the following problems.
C ++ problems in the android JNI environment:1)
Error: Base operand of '-> 'has non-pointer type' _ jnienv'
The calling method of C ++ is different from that of C. Comparison:
C++: env->NewStringUTF("JNI C++!"); C: (*env)->NewStringUTF(env, "JNI C!");
Example:
jstringJava_com_listthrd_MainActivity_runListThrd( JNIEnv* env, jobject thiz ){ // for C++ return env->NewStringUTF("JNI C++!"); // for C// return (*env)->NewStringUTF(env, "JNI C!");}
2) error: iostream: no such file or directory
STL library files such as iostream cannot be found, because STL is not used by ndk by default. Solution:
Create the application. mk file in the JNI directory and add the following content:
#APP_STL:=stlport_staticAPP_STL := gnustl_static
Select either of the preceding two. Gnustl is recommended because it can solve other problems. See later.
3) error: 'terminate' is not a member of 'std'
This error occurs when stlport is used. Use gnustl_static instead.
4) undefined reference to '_ cxa_end_cleanup'
This error occurs when stlport is used. Therefore, use gnustl_static instead.
5) undefined reference to '_ android_log_print'
This is a log library that is not linked to Android. Add the local_ldlibs definition to the Android. mk file as follows:
#LOCAL_EXPORT_LDLIBS := -llogLOCAL_LDLIBS := -llog
Above, local_export_ldlibs is easy to use in C language, which can be removed here.
6) Error/androidruntime (330): Fatal exception: Main
Java. Lang. unsatisfiedlinkerror...
Use the C language-style call convention to add extern before the exported JNI Function
"C" is specified, or the JNI export function is enclosed by curly arc. For example:
extern "C" { JNIEXPORT jstring JNICALL Java_com_listthrd_MainActivity_runListThrd( JNIEnv* env, jobject thiz ) { // ... }}
[Appendix]
1. tinythread ++: http://tinythreadpp.bitsnbites.eu/