如果在user.hpp中定义了boost_NO_EXCEPTIONS,则无法编译boost::shared_ptr的原因
why boost::shared_ptr can not be compiled if BOOST_NO_EXCEPTIONS is defined in user.hpp
我有一个嵌入式系统,想在这个系统中使用boost,但需要禁用异常,因为我不想支付异常的费用。
Boost提供了一个user.hpp和可设置的宏选项Boost_NO_EXCEPTION和Boost_NO_EXCEPTION_STD_NAMESPACE,但如果定义了这两个宏,则无法编译Boost::shared_ptr(更确切地说,无法链接)。
shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a): undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status
为什么boost提供宏选项,但不承诺使用这些选项进行编译?
它可以编译。
它就是无法链接。
这是因为,如果定义BOOST_NO_EXCEPTIONS,则必须在某个地方提供boost::throw_exception(std::exception const&)
的实现,以取代通常的错误引发功能。
阅读throw_exception.hpp:中的评论
namespace boost
{
#ifdef BOOST_NO_EXCEPTIONS
void throw_exception(std::exception const & e); // user defined
#else
//[Not user defined --Dynguss]
template<class E> inline void throw_exception(E const & e)
{
throw e;
}
#endif
} // namespace boost
这是我的最终解决方案。
shared_ptr_boost.cpp:
#include <boost/shared_ptr.hpp>
#include <stdio.h>
#include <exception>
namespace boost{
void throw_exception(std::exception const &e){}
}
int main(){
boost::shared_ptr<int> pName(new int(2));
*pName += 3;
printf("name = %dn", *pName);
return 0;
}
编译命令:
arm-hisiv100-linux-uclibcgnueabi-g++ -I../boost_1_59_0/ -DBOOST_NO_EXCEPTIONS -DBOOST_NO_EXCEPTION_STD_NAMESPACE -fno-exceptions shared_ptr_boost.cpp
对于这样一个小的测试程序,我得到了大约1.7K的可执行文件;这就是我不想为例外而付出的代价。
相关文章:
- 编译 Boost 时在 OS X 上的"ld:未知选项:-soname"
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- 在 Centos7 上使用 boost 1.71.0 编译错误
- 包含来自 boost 1.73.0 的read_graphviz_new.cpp无法编译
- 编译用于Sony NMOS实现的C++boost库
- Simple_ls.cpp的C++Boost编译错误-未找到对Boost的引用::文件系统::路径::文件名
- python的Boost编译错误
- 在 Ubuntu 14.04 上使用 boost 编译:未定义对 'boost::thread::get_id() const' 的引用
- 使用boost编译时出错
- 关于使用cmake和boost编译c++代码的错误
- ubuntu服务器14.04上的Boost编译失败
- 使用boost编译错误.图1.56.0和g++ 4.6.4
- OS X 10.8.5下的Boost编译错误
- Boost编译标记gcc错误
- 使用mingw的boost编译错误
- Boost编译标志,它是什么意思
- Boost编译问题
- 用boost编译一些代码.Python在win7-64位
- 不能用boost编译
- 使用boost编译以使用可用的boost版本