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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010096900/article/details/79561890

简介

在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}")

猜你喜欢

转载自blog.csdn.net/u010096900/article/details/79561890