BOOST应用 无法解析的外部符号 "void __cdecl boost::throw_exception(class std::exception const &)"

简介

在Windows系统上,使用boost时发现未定义的外部符号的链接问题:

thread_test_01.cpp.obj : error LNK2019: 无法解析的外部符号 "void __cdecl boost::
throw_exception(class std::exception const &)" (?throw_exception@boost@@YAXAEBVe
xception@std@@@Z),该符号在函数 "public: __cdecl boost::gregorian::date::date(cl
ass boost::gregorian::greg_year,class boost::gregorian::greg_month,class boost::
gregorian::greg_day)" (??0date@gregorian@boost@@QEAA@Vgreg_year@12@Vgreg_month@1
2@Vgreg_day@12@@Z) 中被引用
thread_test_01.exe : fatal error LNK1120: 1 个无法解析的外部命令
本文总结了该问题的解决思路。

问题分析

搜索代码,发现boost/throw_exception.hpp文件中定义了如下相关的代码:

#ifdef BOOST_NO_EXCEPTIONS

void throw_exception( std::exception const & e ); // user defined

#else

inline void throw_exception_assert_compatibility( std::exception const & ) { }

template<class E> BOOST_NORETURN inline void throw_exception( E const & e )
{
    //All boost exceptions are required to derive from std::exception,
    //to ensure compatibility with BOOST_NO_EXCEPTIONS.
    throw_exception_assert_compatibility(e);

#ifndef BOOST_EXCEPTION_DISABLE
    throw enable_current_exception(enable_error_info(e));
#else
    throw e;
#endif
}

#endif

由于定义了BOOST_NO_EXCEPTIONS宏,用户需要自己实现throw_exception函数。进一步搜索BOOST_NO_EXCEPTIONS宏,发现代码中相关定义如下:

#if !defined(_CPPUNWIND) && !defined(__EXCEPTIONS)
#  define BOOST_NO_EXCEPTIONS
#endif

而宏_CPPUNWIND用于标识编译器是否打开异常处理。而这里需要打开异常处理的功能,在Visual studios中,需要在编译选项中使用/GX或者/EHsc,告诉编译器使能异常处理的功能。如果使用cmake,可以使用下面的设置来进行配置:

set(CMAKE_CXX_FLAGS "/EHsc ${CMAKE_CXX_FLAGS}")

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值