一、出现相似的异常函数错误
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来关闭这个特性节约空间
二、STL模块函数找不到,连接失败
stdc++/include/bits/stl_list.h:466: error: undefined reference to '__cxa_end_catch
stdc++/include/bits/stl_list.h:469: error: undefined reference to '__cxa_rethrow'
nal_baseD1Ev+0x0): error: undefined reference to '__gxx_personality_v0'
这些函数在 libsupc++.a库中,加上便可
ist.h:1424: error: undefined reference to 'std::_List_node_base::unhook()'
stdc++/include/bits/list.tcc:101: error: undefined reference to 'std::_List_node_base::hook(std::_List_node_base*)
这些函数在 libstdc++.a库中,加上便可
在android4.2系统中如此使用:
prebuilt_stdcxx_PATH := prebuilts/ndk/current/sources/cxx-stl/gnu-libstdc++
LOCAL_C_INCLUDES := \
$(prebuilt_stdcxx_PATH)/include \
$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI)/include/ \
LOCAL_CPPFLAGS += -fexceptions -frtti
LOCAL_LDFLAGS += -L$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI) -lgnustl_static -lsupc++
对于头文件的引用及库加上,基本上能够彻底解决STL库的函数 node
对于stl库,其头文件及库在android4.1版本及以上才将全部函数实现彻底,全部若是还碰连接某个函数失败的话,那么能够下载一个android4.1或4.2版本,将android-4.2_r1\prebuilts\ndk\current\sources下面的的代码打包放到prebuilt\ndk下面,在android.mk中对于prebuilt_stdcxx_PATH进行重定义便可。 android
三、预编译静态库: c++
build/core/base_rules.mk:81: * Each module must use a LOCAL_MODULE_TAGS in its
build/core/base_rules.mk:82: * Android.mk. Possible tags declared by a module:
build/core/base_rules.mk:83: *
build/core/base_rules.mk:84: * optional, debug, eng, tests, samples express
修改build\core下的文件definitions.mk 安全
define include-prebuilt
include $$(CLEAR_VARS)
LOCAL_SRC_FILES := $(1)
LOCAL_BUILT_MODULE_STEM := $(1)
LOCAL_MODULE_SUFFIX := $$(suffix $(1))
LOCAL_MODULE := $$(basename $(1))
LOCAL_MODULE_CLASS := $(2)
LOCAL_MODULE_TAGS := optional // 加上这句话便可
include $$(BUILD_PREBUILT)
endef app
预编译静态库指令: eclipse
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
$(call add-prebuilt-files, STATIC_LIBRARIES, libboost_filesystem.a)
$(call add-prebuilt-files, STATIC_LIBRARIES, libboost_system.a)
$(call add-prebuilt-files, STATIC_LIBRARIES, libboost_thread.a) ide