如果在user.hpp中定义了BOOST_NO_EXCEPTIONS,则无法编译boost :: shared_ptr

Liu*_*ick 4 c++ boost

我有一个嵌入式系统,并希望在此系统中使用boost,但需要禁用异常,因为我不想支付异常费用.

Boost给出了一个user.hpp和可设置的宏选项BOOST_NO_EXCEPTIONSBOOST_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
Run Code Online (Sandbox Code Playgroud)

为什么boost会提供宏选项,但不承诺使用这些选项进行编译?

seh*_*ehe 9

它可以编译.

它只是无法链接.

那是因为如果你定义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
Run Code Online (Sandbox Code Playgroud)