歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 編譯STL模塊相關錯誤解決

Android 編譯STL模塊相關錯誤解決

日期:2017/3/1 10:01:05   编辑:Linux編程

由於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來關閉這個特性節約空間

Copyright © Linux教程網 All Rights Reserved