Android 编译STL模块相关错误解决


由于Android系统目前没有将boost加入,这里面使用了大量的STL及C++的一些语言特性,导致编译出现令人非常头痛的问题。

1、出现类似的异常函数错误
boost/exception/detail/exception_ptr.hpp:382: error: expected ';' before 'catch'
boost/exception/detail/exception_ptr.hpp:387: error: expected primary-expression before 'catch

boost/date_time/constrained_value.hpp:110: error: invalid initialization of reference of type 'const std::exception&' from expression of type 'boost::CV::simple_exception_policy<short unsigned int, 1u, 366u, boost::gregorian::bad_day_of_year>::exception_wrapper'
boost/throw_exception.hpp:48: error: in passing argument 1 of 'void boost::throw_exception(const std::exception&)'


解决方案:
此问题的出现是编译器的异常异常捕获被禁用了,需要在Android.mk文件中开启。
在Android.mk文件中添加:LOCAL_CPPFLAGS += -fexceptions就可以了。
或者在Application.mk文件中添加APP_CPPFLAGS += -fexceptions也是可以的(eclipse下编译)
并且android平台提供了一个最小化的C++运行库(/system/lib/libstdc++)以及与之对应的头文件。


原因:
只有异常安全的代码才应该用-fexceptions编译吧(这在编译C++的时候是默认选项)。
绝大部分C代码都不是异常安全的,如果没加-fexceptions,异常经过的时候程序会直接退出,加了-fexceptions以后,万一它调用的某个函数抛出了异常,
也会直接经过这段代码,弄不好会出现内存泄漏之类的问题而不报错吧。
所以对于try{}catch{}的关键字使用时需要加上 -fexceptions

-frtti:
打开rtti(runtime type identification)?这样编译器会为每个有虚函数的类添加一些信息以支持rtti特性,例如dynamic_cast typeid之类.
可以使用-fno-rtti来关闭这个特性节约空间

  • 1
  • 2
  • 3
  • 下一页

相关内容