您的位置:首页 > 移动开发 > Android开发

如何移植library到android

2011-01-05 19:56 519 查看
http://bloodysofiya.blog.163.com/blog/static/116562343201022593152405/

最近两天,我在做移植工作。可怜,对方给的source code 不完全,很多东西要自己去搞定。
分享一下移植经验,虽然我没有成功。

首先说明一下,Android Bionic C library ,他的设计是尽量简单。
这意味这个C库只围绕着内核提供轻量级的包装,我们让它尽量小,不去处理一些细枝末页的事情。取Bionic这个名字,是因为它由部分BSD和部分linux组成。
就因为这个原因,导致我的移植工作很困难.
我在link时候的错误,事实上在编译的时候就错了,但是我改了头文件,把这些函数声明都加上去了,所以在link的时候报错了:
"undefined reference to 'setmntent'"
"undefined reference to 'endmntent'"
"undefined reference to 'ftime'"
都是因为Bionic C library没有提供实现。要解决这些问题,看来是只能自己加上实现的代码,重新用android toolchain 去编译.
然后,我在说一说Android toolchain,目前最新的toolchain最新是20090323的,android ndk r3(Goto:http://developer.android.com/sdk/ndk/index.html)用的就是这个。这个toolchain是不支持STL的,可以看google group上的讨论,Android程序开发人David turner说:I confirm that the Android framework has been specifically designed to not need any STL / C++ exceptions / RTTI.This was a deliberate decision at the time the platform was first designed, because the available compiler and STL implementations at the time were very
poor and generated humongous code that ran very poorly on the puny ARM CPUs of the time. And all of this has been benchmarked a long time ago and I don't have numbers to share. 总之,说了一堆原因NND不支持STL,取而代之的是ASTL.如果你的代码含有stl,那么恭喜你android toolchain 目前是没办法帮你搞定的。幸运的是,Crystax解决了这个问题(Goto:http://www.crystax.net/android/ndk-r3.php)
即使这样,我仍然发现有问题,我举个简单的例子:
"undefined reference to 'std::_Rb_tree_rebalance_for_erase(std::_Rb_tree_node_base* ........)'"
std::_Rb_tree_rebalance_for_erase理论上应该在libstdc++.so里有实现,但是很不幸,android的没有。所以,我们只能加上实现,再次重编译stdc++。
最后,我不得不提一下,link要注意顺序ld 库的次序,不然很可能又会error.

更新,其实也不用全部编译bionic c library来支持新的功能,我们可以写一个.c档,然后实现setmntent,endmntent,ftime这些函数,用arm-eabi-gcc 编译出.o档,再使用arm-eabi-ar把.o档加到libc.a里面。同理可以生成libstdc++.a文件.
关于"undefine reference __cxa-atexit"的错误,我们先看一下它的解释.
-fuse-cxa-atexitRegister destructors for objects with static storage duration with the __cxa_atexit function rather than the atexitfunction. This option is required for fully standards-compliant handling of static destructors, but will only work if your C library supports __cxa_atexit.首先,--cxa-atexit是一个C++ language option,然后要求C库要支持__cxa_atexit。所以,我们找到原因拉,android bionic c library 不支持__cxa_atexit。--cxa-atexit函数的作用是In any event, GCC calls static object destructors andatexit handlers from a shared object during its final dlclose call, or during exit

再次更新,目前我的东西只剩下一个link的错误了,这个错误,不能再简单的添加实现了,这个错误是libsupc++.a的问题,我只能重新编译它啦,但是重新编译它有又遇到了很大的困难。

okay,简单测试通过拉.原来不是libsupc++.a的问题,是他依赖的libc.a(之前我不小心用成了libc.so,静态库和动态库真的不能乱用啊!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: