在PC上的C++开发中,boost已经很普遍。但对于Android这样的移动平台呢?由于KlayGE正在移植Android,作为依赖库之一的boost也必须移植过去。官方的boost并没有提供Android支持,看来得自己做了。
受MysticTreeGames的Boost-for-Android启发,我想用最新的Crystax NDK来编译boost 1.47。
准备工作
需要下载
补丁
首先,MysticTreeGames的补丁是基于boost 1.45和官方的NDK r5,在最新版上不一定能完美运作。因为有些源文件在1.45到1.47的过程中更改了,无法自动打上补丁。所以我手工地在原始的boost 1.47上根据补丁进行修改。其实不太难,因为只有12个文件需要修改。
编译脚本
MysticTreeGames提供的build-android.sh也没法直接用,很可能是因为它并没有在Cygwin上测试过。于是我写了一个build_android.sh和一个build_android.bat作为编译脚本。幸运的是,最终的脚本很简单,而且在Windows、Cygwin和Linux下都能用。(还有个额外的好处,用build_android.bat的话就不再需要Cygwin了!)
到这里,boost 1.47已经可以用NDK r6正常编译了。但有个缺点是只能生成静态库(”link=static runtime-link=static”)。
还能用VC编译吗?
接着我试用VC10来编译boost的Windows版,结果出问题了。由于user-config.jam被打了补丁,现在它只能支持NDK的编译器。解决方法是把修改过的user-config.jam拷到boost的根目录,重命名为别的名字,比如user-config-android.jam。然后就可以用“bjam –user-config=./user-config-android.jam …”来告诉bjam用哪个.jam来作为user config。
合在一起
在附件里面你可以找到所有我修改过的文件。在编译之前,你必须设置一个环境变量,名字为“ANDROID_NDK”,只想你的NDK根目录。
Boost is commonly used in developing a PC program in C++. But if the target is a mobile platform such as Android? We are working on porting KlayGE to Android in these days. As a dependent library, boost must be ported, too. The official boost doesn’t support Android, So I have to do my own.
Inspired by MysticTreeGames’s Boost-for-Android project, I tried to get boost 1.47 compiled with the latest NDK from Crystax.
Preparation
Some packages must be downloaded
Patches
First of all, the MysticTreeGames’s patch is based on boost 1.45 and official NDK r5. It’s probably not work perfectly on the latest version. Because some source files are changed from 1.45 to 1.47, the patch may failed. So I have to manually modify all places in the original boost 1.47, follow by the patches. It’s not difficult because there are only 12 patches.
Script
MysticTreeGames’s build-android.sh from Boost-for-Android also failed. Probably because it’s not tested well under Cygwin. I wrote a build_android.sh and a build_android.bat for workaround. Fortunately it’s simple and works well on Windows, Cygwin and Linux. (A bonus is that you don’t need the Cygwin any more if using build_android.bat!)
After that, the boost 1.47 can be compiled by NDK r6. The limitation is we can only use “link=static runtime-link=static”.
Build by VC again?
When I tried to use VC10 to rebuild boost for Windows, a problem shows up. The user-config.jam is patched so that it can only work with NDK’s compiler. The solution is move the modified user-config.jam to the root folder of boost, renamed it into some other name, such as user-config-android.jam. Then you can use “bjam –user-config=./user-config-android.jam …” to tell bjam use this .jam file as user config.
All in one
You can find my modified files in the attachment. Before compiling, you need to set an environment variable name “ANDROID_NDK”, which is point to your NDK’s root.
boost_for_android_patch.7z
Comments