Solution for missing std::wstring support in Android NDK?

人走茶凉 提交于 2019-12-30 09:30:17

问题


I have a game which uses std::wstring as its basic string type in thousand of places as well as doing operations with wchar_t and its functions: wcsicmp() wcslen() vsprintf(), etc.

The problem is wstring is not supported in R5c (latest ndk at the time of this writting).

I can't change the code to use std::string because of internationalization and I would be breaking the game engine which is used by many games ...

Which options do I have?

1 - Replace string and wstring with my own string classes

This would give me better platform independency, but it is ridiculous to reimplement the wheel. I've already started with a COW implementation of strings. I need it to be COW because I use them as keys in hash_maps. This is of course lots of work and error prone ... but it seems it is something I can do.

2 - Try to fix the NDK recompiling the STLPort with my own implementations of the wide char string functions of the C standart library (wcslen, mbstowcs ... )

This would be the preferable way ... but I have no idea how to do it :(

How do I replace a function (lets say wcslen) in the libstdc++.a or libstlport_static.a? (not sure where they are :()

And as well I'm not sure which functions I need to reimplement, I know wcslen is not working so I guess they should be all ...

3 - Do you have any other idea?

I can't wait for an official fix for this and I will have to go with option #1 if I can't realize how to do #2.

I've read somewhere that if you target 2.3 you can use wstrings, but I ought to target Android 2.1.

PS: Forgot to say I need to use STL of course, but no RTTI and I can live without exceptions.

Thanks in advance!


回答1:


Try out CrystaX's NDK. It has had stl support long before the official google one. The current version (r5), which is based off the of the official ndk r5, is still beta 3, but it does have wchar_t support.

http://www.crystax.net/android/ndk-r5.php




回答2:


I'm suffering from the same problem as you, but my only other thought is to load the strings via the JNI (as jstring* in native land), then convert them to UTF characters as necessary. Take a look at the available JNI string functions here:
http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#string_operations




回答3:


Qt provides an excellent copy-on-write, international-friendly string implementation, QString, that is LGPLed.

You could, in theory extract it from the Qt source and use it in your own project. You will find the QString implementation in src/corelib/tools/qstring.h and .cpp in a Qt source download. You would also need the QChar, QByteArray, QAtomic, and QNamespace includes/classes (all under the corelib folder,) and you should define QT_NO_STL_WCHAR when compiling. (For this I would compile by hand or using my own script/Makefile.) Not simple, but once you get it up and running your life will be a lot simpler. It's better than reinventing the wheel, because it comes with loads of convenience functions and features.

Rather than stripping out just QString, you could also just use the QtCore module as a whole. See the android-lighthouse project for a Qt port to Android. (Also, it might be better to get your sources from there than from the above "vanilla" link, regardless of what you do.)



来源:https://stackoverflow.com/questions/6385643/solution-for-missing-stdwstring-support-in-android-ndk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!